seiten 0.0.8 → 1.0.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.
Files changed (104) hide show
  1. checksums.yaml +5 -5
  2. data/Rakefile +1 -0
  3. data/app/controllers/seiten/pages_controller.rb +2 -18
  4. data/config/initializers/seiten.rb +1 -1
  5. data/lib/seiten/errors/base_error.rb +4 -0
  6. data/lib/seiten/errors/page_error.rb +4 -0
  7. data/lib/seiten/errors/routing_error.rb +4 -0
  8. data/lib/seiten/helpers/backend.rb +23 -0
  9. data/lib/seiten/helpers/current.rb +33 -0
  10. data/lib/seiten/helpers/frontend.rb +19 -0
  11. data/lib/seiten/html/breadcrumb.rb +41 -0
  12. data/lib/seiten/html/helpers.rb +32 -0
  13. data/lib/seiten/html/navigation.rb +39 -0
  14. data/lib/seiten/navigation.rb +43 -0
  15. data/lib/seiten/page.rb +71 -50
  16. data/lib/seiten/page_collection.rb +54 -0
  17. data/lib/seiten/page_collection_builder.rb +63 -0
  18. data/lib/seiten/railtie.rb +7 -0
  19. data/lib/seiten/routes_helper.rb +16 -11
  20. data/lib/seiten/slug_builder.rb +32 -0
  21. data/lib/seiten/version.rb +1 -1
  22. data/lib/seiten.rb +63 -12
  23. data/test/controllers/helper_methods_test.rb +25 -0
  24. data/test/controllers/pages_controller_test.rb +9 -0
  25. data/test/controllers/posts_controller_test.rb +12 -0
  26. data/test/controllers/seiten/pages_controller_test.rb +39 -0
  27. data/test/dummy/app/controllers/application_controller.rb +10 -0
  28. data/test/dummy/app/controllers/pages_controller.rb +1 -1
  29. data/test/dummy/app/controllers/posts_controller.rb +10 -0
  30. data/test/dummy/app/helpers/application_helper.rb +1 -0
  31. data/test/dummy/app/pages/{de → application/de}/produkte.html.erb +0 -0
  32. data/test/dummy/app/pages/{en/about/our-team/switzerland.html.erb → application/en/about/our-team/italy.html.erb} +0 -0
  33. data/test/dummy/app/pages/{en/about/works.html.erb → application/en/about/our-team/switzerland.html.erb} +0 -0
  34. data/test/dummy/app/pages/application/en/about/our-team.html.erb +2 -0
  35. data/test/dummy/app/pages/application/en/about/partners.html.erb +1 -0
  36. data/test/dummy/app/pages/{en/contact.html.erb → application/en/about/works.html.erb} +0 -0
  37. data/test/dummy/app/pages/{en/about/our-team/italy.html.erb → application/en/about.html.erb} +0 -0
  38. data/test/dummy/app/pages/{en/products/logo-design.html.erb → application/en/contact.html.erb} +0 -0
  39. data/test/dummy/app/pages/{en → application/en}/home.html.erb +0 -0
  40. data/test/dummy/app/pages/{en/products/web-development.html.erb → application/en/products/hire-us.html.erb} +0 -0
  41. data/test/dummy/{db/test.sqlite3 → app/pages/application/en/products/logo-design.html.erb} +0 -0
  42. data/test/dummy/app/pages/application/en/products/web-development.html.erb +0 -0
  43. data/test/dummy/app/pages/{en → application/en}/products.html.erb +0 -0
  44. data/test/dummy/app/pages/{localization.html.erb → application/localization.html.erb} +0 -0
  45. data/test/dummy/app/pages/help/en/home.html.erb +0 -0
  46. data/test/dummy/app/pages/help/en/logging-in.html.erb +0 -0
  47. data/test/dummy/app/views/layouts/application.html.erb +1 -1
  48. data/test/dummy/app/views/layouts/home.html.erb +1 -1
  49. data/test/dummy/app/views/pages/secret.html.erb +1 -1
  50. data/test/dummy/app/views/posts/index.html.erb +1 -0
  51. data/test/dummy/config/application.rb +4 -41
  52. data/test/dummy/config/boot.rb +2 -9
  53. data/test/dummy/config/cable.yml +9 -0
  54. data/test/dummy/config/environments/development.rb +26 -21
  55. data/test/dummy/config/environments/production.rb +3 -0
  56. data/test/dummy/config/environments/test.rb +15 -19
  57. data/test/dummy/config/initializers/application_controller_renderer.rb +6 -0
  58. data/test/dummy/config/initializers/cookies_serializer.rb +5 -0
  59. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  60. data/test/dummy/config/initializers/inflections.rb +6 -5
  61. data/test/dummy/config/initializers/mime_types.rb +0 -1
  62. data/test/dummy/config/initializers/new_framework_defaults.rb +20 -0
  63. data/test/dummy/config/initializers/session_store.rb +1 -6
  64. data/test/dummy/config/initializers/wrap_parameters.rb +5 -5
  65. data/test/dummy/config/{navigation/de.yml → navigations/application.de.yml} +5 -5
  66. data/test/dummy/config/{navigation/en.yml → navigations/application.en.yml} +15 -7
  67. data/test/dummy/config/navigations/help.en.yml +17 -0
  68. data/test/dummy/config/routes.rb +6 -3
  69. data/test/dummy/config/secrets.yml +22 -0
  70. data/test/dummy/log/development.log +1716 -4559
  71. data/test/dummy/log/test.log +43833 -16254
  72. data/test/fixtures/breadcrumb.html +1 -0
  73. data/test/fixtures/navigation.html +1 -0
  74. data/test/integration/layout_test.rb +10 -11
  75. data/test/integration/navigation_test.rb +50 -50
  76. data/test/lib/seiten/helpers/frontend_test.rb +37 -0
  77. data/test/lib/seiten/html/breadcrumb_test.rb +29 -0
  78. data/test/lib/seiten/html/helpers_test.rb +55 -0
  79. data/test/lib/seiten/html/navigation_test.rb +29 -0
  80. data/test/lib/seiten/navigation_test.rb +45 -0
  81. data/test/lib/seiten/page_collection_builder_test.rb +141 -0
  82. data/test/lib/seiten/page_collection_test.rb +59 -0
  83. data/test/lib/seiten/page_test.rb +188 -0
  84. data/test/lib/seiten/slug_builder_test.rb +47 -0
  85. data/test/lib/seiten_test.rb +16 -0
  86. data/test/test_helper.rb +2 -8
  87. metadata +134 -78
  88. data/app/helpers/seiten_helper.rb +0 -46
  89. data/lib/seiten/controllers/helpers.rb +0 -16
  90. data/lib/seiten/page_store.rb +0 -165
  91. data/test/dummy/app/assets/javascripts/application.js +0 -15
  92. data/test/dummy/app/assets/stylesheets/application.css +0 -13
  93. data/test/dummy/app/pages/en/about/our-team.html.erb +0 -2
  94. data/test/dummy/app/pages/en/about/partners.html.erb +0 -1
  95. data/test/dummy/config/navigation.yml +0 -37
  96. data/test/dummy/db/development.sqlite3 +0 -0
  97. data/test/dummy/tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953 +0 -0
  98. data/test/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
  99. data/test/dummy/tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6 +0 -0
  100. data/test/integration/i18n_test.rb +0 -31
  101. data/test/integration/redirect_test.rb +0 -15
  102. data/test/page_store_test.rb +0 -28
  103. data/test/page_test.rb +0 -104
  104. data/test/seiten_test.rb +0 -11
@@ -0,0 +1 @@
1
+ <ul class="breadcrumb"><li class="breadcrumb__item"><a href="/our-team">About</a></li><li class="breadcrumb__item"><span class="breadcrumb__separator">&gt;</span><a href="/about/our-team">Our Team</a></li><li class="breadcrumb__item breadcrumb__item--current"><span class="breadcrumb__separator">&gt;</span><a href="/about/our-team/switzerland">Switzerland</a></li></ul>
@@ -0,0 +1 @@
1
+ <ul class="navigation"><li class="navigation__item navigation__item--active navigation__item--current test-class" data-behaviour="hello"><a href="/">Home</a></li><li class="navigation__item"><a href="/blog">Blog</a></li><li class="navigation__item navigation__item--parent"><a href="/products">Products</a><ul class="navigation__nodes"><li class="navigation__item"><a href="/products/logo-design">Logo Design</a></li><li class="navigation__item"><a href="/products/web-development">Web Development</a></li><li class="navigation__item"><a href="/contact">Hire us</a></li></ul></li><li class="navigation__item navigation__item--parent"><a href="/our-team">About</a><ul class="navigation__nodes"><li class="navigation__item navigation__item--parent"><a href="/about/our-team">Our Team</a></li><li class="navigation__item"><a href="/about/works">Works</a></li><li class="navigation__item navigation__item--parent"><a href="/about/partners">Partners</a></li></ul></li><li class="navigation__item"><a href="/contact">Contact</a></li><li class="navigation__item"><a href="/localization">I18n</a></li></ul>
@@ -1,26 +1,25 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class LayoutTest < ActionDispatch::IntegrationTest
4
-
5
4
  def test_returns_default_layout
6
- visit "/about/works"
7
- assert has_content?("Default Layout"), "not rendered default layout"
5
+ get '/about/works'
6
+ assert_select 'p', 'Default Layout'
8
7
  end
9
8
 
10
9
  def test_returns_home_layout
11
- visit "/"
12
- assert has_content?("Home Layout"), "not rendered home layout"
10
+ get '/'
11
+ assert_select 'p', 'Home Layout'
13
12
  end
14
13
 
15
14
  def test_returns_inherited_layout
16
- visit "/products/logo-design"
17
- assert has_content?("Products Layout"), "not rendered inherited layout"
15
+ get '/products/logo-design'
16
+ assert_select 'p', 'Products Layout'
18
17
  end
19
18
 
20
19
  def test_returns_no_inherited_layout
21
- visit "/about/our-team/switzerland"
22
- assert has_content?("Default Layout"), "not rendered default layout"
23
- visit "/contact"
24
- assert has_content?("Default Layout"), "not rendered default layout"
20
+ get '/about/our-team/switzerland'
21
+ assert_select 'p', 'Default Layout'
22
+ get '/contact'
23
+ assert_select 'p', 'Default Layout'
25
24
  end
26
25
  end
@@ -1,69 +1,69 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class NavigationTest < ActionDispatch::IntegrationTest
4
-
5
- def test_returns_2_level_deep_navigation
6
- visit "/"
7
- assert has_content?("Home")
8
- assert has_content?("Products")
9
- assert has_content?("Logo Design")
10
- assert has_content?("Web Development")
11
- assert has_content?("Hire us")
12
- assert has_content?("About")
13
- assert has_content?("Our Team")
14
- assert has_content?("Works")
15
- assert has_content?("Partners")
16
- assert has_content?("Contact")
4
+ def test_should_ignore_active_storage_routes
5
+ assert_raise ActionController::RoutingError do
6
+ get '/rails/active_storage/test'
7
+ end
17
8
  end
18
9
 
19
- def test_does_not_return_3_level_deep_pages
20
- visit "/"
21
- assert !has_content?("Daniel Puglisi")
22
- assert !has_content?("Codegestalt")
23
- assert !has_content?("Kreatify")
10
+ def test_should_raise_seiten_routing_error
11
+ assert_raise Seiten::Errors::RoutingError do
12
+ get '/non-existing-route'
13
+ end
24
14
  end
25
15
 
26
- def test_returns_home_url
27
- visit "/contact"
28
- assert_equal "/contact", current_path
29
- click_link "Home"
30
- assert_equal "/", current_path
16
+ def test_returns_2_level_deep_navigation
17
+ get '/'
18
+ assert_select 'a', 'Home'
19
+ assert_select 'a', 'Products'
20
+ assert_select 'a', 'Logo Design'
21
+ assert_select 'a', 'Web Development'
22
+ assert_select 'a', 'Hire us'
23
+ assert_select 'a', 'About'
24
+ assert_select 'a', 'Our Team'
25
+ assert_select 'a', 'Works'
26
+ assert_select 'a', 'Partners'
27
+ assert_select 'a', 'Contact'
28
+ assert_select 'a', { count: 0, text: 'Daniel Puglisi' }
29
+ assert_select 'a', { count: 0, text: 'Codegestalt' }
30
+ assert_select 'a', { count: 0, text: 'Kreatify' }
31
+ assert_select '.navigation__item--active', count: 1
32
+ assert_select 'title', 'My awesome Web Agency'
31
33
  end
32
34
 
33
35
  def test_returns_sub_navigation
34
- visit "/about/partners"
35
- assert has_content?("Daniel Puglisi")
36
- assert has_content?("Codegestalt")
37
- assert has_content?("Kreatify")
36
+ get '/about/partners'
37
+ assert_select 'a', 'Daniel Puglisi'
38
+ assert_select 'a', 'Codegestalt'
39
+ assert_select 'a', 'Kreatify'
40
+ assert_select '.navigation__item--active', count: 2
41
+ assert_select 'title', 'Partners'
38
42
  end
39
43
 
40
44
  def test_returns_breadcrumb_navigation
41
- visit "/about/our-team/switzerland"
42
- assert has_content?("> About")
43
- assert has_content?("> Our Team")
44
- assert has_content?("> Switzerland")
45
- end
46
-
47
- def test_returns_clicked_breadcrumb_page
48
- visit "/about/our-team/switzerland"
49
- find(".breadcrumb a", text: "Our Team").click
50
- assert_equal "/about/our-team", current_path
45
+ get '/about/our-team/switzerland'
46
+ assert_select '.breadcrumb a', 'About'
47
+ assert_select '.breadcrumb a', 'Our Team'
48
+ assert_select '.breadcrumb a', 'Switzerland'
49
+ assert_select '.navigation__item--active', count: 2
50
+ assert_select '.breadcrumb__item--current', count: 1
51
+ assert_select 'title', 'Switzerland'
51
52
  end
52
53
 
53
- def test_returns_clicked_navigation_page
54
- visit "/"
55
- click_link "Web Development"
56
- assert_equal "/products/web-development", current_path
57
- end
58
-
59
- def test_returns_external_links
60
- visit "/about/partners"
61
- assert has_xpath?("//a[@href='http://danielpuglisi.com']")
54
+ def test_returns_page_with_navigation_which_is_not_defined_in_navigation_config
55
+ get '/secret'
56
+ assert_response :success
57
+ assert_select 'p', 'pages#secret'
58
+ assert_select '.navigation__item--active', count: 0
59
+ assert_select 'title', nil
62
60
  end
63
61
 
64
- def test_returns_page_with_navigation_which_is_not_defined_in_navigation_config
65
- visit "/secret"
66
- assert_equal 200, status_code
67
- has_content?("This is a secret page")
62
+ def test_returns_current_page_of_custom_controller
63
+ get '/blog'
64
+ assert_select 'title', 'Blog'
65
+ assert_select 'p', 'posts#index'
66
+ assert_select '.navigation__item--active', count: 1
67
+ assert_select '.navigation__item--active a', text: 'Blog'
68
68
  end
69
69
  end
@@ -0,0 +1,37 @@
1
+ require 'test_helper'
2
+
3
+ class Seiten::Helpers::FrontendTest < ActionView::TestCase
4
+ include Seiten::Helpers::Frontend
5
+
6
+ def navigation
7
+ @navigation ||= Seiten::Navigation.find_by(id: 'application.en')
8
+ end
9
+
10
+ def current_navigation
11
+ @current_navigation ||= navigation
12
+ end
13
+
14
+ def current_page
15
+ @current_page
16
+ end
17
+
18
+ test '#link_to_seiten_page' do
19
+ page = current_navigation.pages.find_by(slug: 'about/our-team')
20
+
21
+ assert_equal '<a href="/about/our-team">Our Team</a>', link_to_seiten_page(page)
22
+ end
23
+
24
+ test '#seiten_navigation' do
25
+ @current_page = current_navigation.pages.find_by(slug: '')
26
+ html = File.read(File.join(FIXTURES_DIR, 'navigation.html')).strip
27
+
28
+ assert_equal html, seiten_navigation
29
+ end
30
+
31
+ test '#seiten_breadcrumb' do
32
+ @current_page = current_navigation.pages.find_by(slug: 'about/our-team/switzerland')
33
+ html = File.read(File.join(FIXTURES_DIR, 'breadcrumb.html')).strip
34
+
35
+ assert_equal html, seiten_breadcrumb
36
+ end
37
+ end
@@ -0,0 +1,29 @@
1
+ require 'test_helper'
2
+
3
+ class Seiten::HTML::BreadcrumbTest < ActiveSupport::TestCase
4
+ def context
5
+ controller = ApplicationController.new
6
+ controller.view_context.tap do |context|
7
+ context.instance_eval do
8
+ def url_options
9
+ { host: 'localhost:3000' }
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ def navigation
16
+ @navigation ||= Seiten::Navigation.find_by(id: 'application.en')
17
+ end
18
+
19
+ def current_page
20
+ @current_page ||= navigation.pages.find_by(slug: 'about/our-team/switzerland')
21
+ end
22
+
23
+ test '::new' do
24
+ nav = Seiten::HTML::Breadcrumb.new(context, page: current_page)
25
+ html = File.read(File.join(FIXTURES_DIR, 'breadcrumb.html')).strip
26
+
27
+ assert_equal html, nav.body
28
+ end
29
+ end
@@ -0,0 +1,55 @@
1
+ require 'test_helper'
2
+
3
+ class Seiten::HTML::HelpersTest < ActiveSupport::TestCase
4
+
5
+ def navigation
6
+ @navigation ||= Seiten::Navigation.find_by(id: 'application.en')
7
+ end
8
+
9
+ def current_page
10
+ @current_page ||= navigation.pages.find(1)
11
+ end
12
+
13
+ def helpers
14
+ Seiten::HTML::Helpers
15
+ end
16
+
17
+ test '#build_page_modifiers' do
18
+ page_1 = navigation.pages.new(id: 99)
19
+ page_2 = navigation.pages.new(id: 200, parent_id: 99)
20
+ assert_equal [:parent], helpers.build_page_modifiers(page_1, nil)
21
+ assert_equal [], helpers.build_page_modifiers(page_2, nil)
22
+
23
+ assert_equal [:parent, :active, :current], helpers.build_page_modifiers(page_1, page_1)
24
+ assert_equal [], helpers.build_page_modifiers(page_2, page_1)
25
+
26
+ assert_equal [:parent, :active, :expanded], helpers.build_page_modifiers(page_1, page_2)
27
+ assert_equal [:active, :current], helpers.build_page_modifiers(page_2, page_2)
28
+ end
29
+
30
+ test '#build_classes' do
31
+ class_options = Seiten.config[:html][:navigation].dup
32
+ modifier_options = Seiten.config[:html][:modifier].dup
33
+
34
+ assert_equal 'navigation', helpers.build_classes(class_options: class_options, modifier_options: modifier_options)
35
+ assert_equal 'navigation__item', helpers.build_classes(:item, class_options: class_options, modifier_options: modifier_options)
36
+ assert_equal 'navigation__item navigation__item--active', helpers.build_classes(:item, modifiers: [:active], class_options: class_options, modifier_options: modifier_options)
37
+ assert_equal 'navigation__item navigation__item--active navigation__item--parent', helpers.build_classes(:item, modifiers: [:active, :parent], class_options: class_options, modifier_options: modifier_options)
38
+ assert_equal 'navigation__nodes navigation__nodes--active navigation__nodes--parent', helpers.build_classes(:nodes, modifiers: [:active, :parent], class_options: class_options, modifier_options: modifier_options)
39
+
40
+ # Works with custom html class config
41
+ class_options[:base] = 'navbar'
42
+ class_options[:item] = 'navbar-item'
43
+ class_options[:nodes] = 'navbar-dropdown'
44
+ modifier_options[:base] = 'is'
45
+ modifier_options[:separator] = '-'
46
+ assert_equal 'navbar', helpers.build_classes(class_options: class_options, modifier_options: modifier_options)
47
+ assert_equal 'navbar-item', helpers.build_classes(:item, class_options: class_options, modifier_options: modifier_options)
48
+ assert_equal 'navbar-item is-active', helpers.build_classes(:item, modifiers: [:active], class_options: class_options, modifier_options: modifier_options)
49
+ assert_equal 'navbar-item is-active is-parent', helpers.build_classes(:item, modifiers: [:active, :parent], class_options: class_options, modifier_options: modifier_options)
50
+ assert_equal 'navbar-dropdown is-active is-parent', helpers.build_classes(:nodes, modifiers: [:active, :parent], class_options: class_options, modifier_options: modifier_options)
51
+
52
+ # Merges extension-class
53
+ assert_equal 'navbar-item is-active is-parent extension-class', helpers.build_classes(:item, modifiers: [:active, :parent], merge: 'extension-class', class_options: class_options, modifier_options: modifier_options)
54
+ end
55
+ end
@@ -0,0 +1,29 @@
1
+ require 'test_helper'
2
+
3
+ class Seiten::HTML::NavigationTest < ActiveSupport::TestCase
4
+ def context
5
+ controller = ApplicationController.new
6
+ controller.view_context.tap do |context|
7
+ context.instance_eval do
8
+ def url_options
9
+ { host: 'localhost:3000' }
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ def navigation
16
+ @navigation ||= Seiten::Navigation.find_by(id: 'application.en')
17
+ end
18
+
19
+ def current_page
20
+ @current_page ||= navigation.pages.find_by(slug: '')
21
+ end
22
+
23
+ test '::new' do
24
+ nav = Seiten::HTML::Navigation.new(context, navigation: navigation, current_page: current_page)
25
+ html = File.read(File.join(FIXTURES_DIR, 'navigation.html')).strip
26
+
27
+ assert_equal html, nav.body
28
+ end
29
+ end
@@ -0,0 +1,45 @@
1
+ require 'test_helper'
2
+
3
+ class Seiten::NavigationTest < ActiveSupport::TestCase
4
+
5
+ def navigation
6
+ @navigation ||= Seiten::Navigation.new(
7
+ name: 'test',
8
+ locale: 'en'
9
+ )
10
+ end
11
+
12
+ test '::find_by' do
13
+ assert_equal 'application.en', Seiten::Navigation.find_by(name: 'application', locale: 'en').id
14
+ assert_equal 'application.de', Seiten::Navigation.find_by(name: 'application', locale: 'de').id
15
+ assert_equal 'application.en', Seiten::Navigation.find_by(locale: 'en').id
16
+ assert_equal 'application.de', Seiten::Navigation.find_by(locale: 'de').id
17
+ end
18
+
19
+ test '::where' do
20
+ assert_equal ['application.de', 'application.en'], Seiten::Navigation.where(name: 'application').map(&:id).sort
21
+ end
22
+
23
+
24
+ test '#id' do
25
+ nav = Seiten::Navigation.new(name: 'site', locale: 'en')
26
+ assert_equal 'site.en', nav.id
27
+ end
28
+
29
+ test '#pages' do
30
+ nav = Seiten::Navigation.new(name: 'test', locale: 'en')
31
+ pages = Seiten::PageCollection.new
32
+ nav.page_collection = pages
33
+
34
+ assert_equal pages, nav.pages
35
+ end
36
+
37
+ test '#pages=' do
38
+ page = Seiten::Page.new
39
+
40
+ navigation.pages = [page]
41
+
42
+ assert_equal page.navigation_id, navigation.id
43
+ assert_equal [page], navigation.pages.all
44
+ end
45
+ end
@@ -0,0 +1,141 @@
1
+ require 'test_helper'
2
+
3
+ class Seiten::PageCollectionBuilderTest < ActiveSupport::TestCase
4
+
5
+ def page_collection
6
+ @page_collection ||= Seiten::PageCollection.new
7
+ end
8
+
9
+ def options
10
+ @options ||= Hash[:pages, []]
11
+ end
12
+
13
+ test 'should build root page' do
14
+ options[:pages] << { "title" => "Home", "root" => true }
15
+ Seiten::PageCollectionBuilder.call(page_collection, options)
16
+
17
+ assert_equal "Home", page_collection.find_by(slug: '').title
18
+ end
19
+
20
+ test 'should build page with a defined url' do
21
+ options[:pages] << { "title" => "About us", "url" => 'about' }
22
+ Seiten::PageCollectionBuilder.call(page_collection, options)
23
+
24
+ assert_equal "About us", page_collection.find_by(slug: 'about').title
25
+ end
26
+
27
+ test 'should build a child page with a defined url' do
28
+ options[:pages] << { "title" => "About us", "nodes" => [{ "title" => "Team", "url" => "/team" }] }
29
+ Seiten::PageCollectionBuilder.call(page_collection, options)
30
+
31
+ assert_equal "Team", page_collection.find_by(slug: 'team').title
32
+ end
33
+
34
+ test 'should build page without an url' do
35
+ options[:pages] << { "title" => "About us", "url" => false }
36
+ Seiten::PageCollectionBuilder.call(page_collection, options)
37
+
38
+ assert_equal "About us", page_collection.where(slug: nil).first.title
39
+ end
40
+
41
+ test 'should build child page' do
42
+ options[:pages] << { "title" => "About us", "nodes" => [{ "title" => "Team" }] }
43
+ Seiten::PageCollectionBuilder.call(page_collection, options)
44
+
45
+ assert_equal "About us", page_collection.find_by(slug: 'about-us').title
46
+ assert_equal "Team", page_collection.find_by(slug: 'about-us/team').title
47
+ end
48
+
49
+ test 'should build child page of child page' do
50
+ options[:pages] << { "title" => "About us", "nodes" => [{ "title" => "Team", "nodes" => [{ "title" => "Management" }] }] }
51
+ Seiten::PageCollectionBuilder.call(page_collection, options)
52
+
53
+ parent = page_collection.find_by(slug: 'about-us')
54
+ child_1 = page_collection.find_by(slug: 'about-us/team')
55
+ child_2 = page_collection.find_by(slug: 'about-us/team/management')
56
+
57
+ assert_equal "About us", parent.title
58
+ assert_equal "Team", child_1.title
59
+ assert_equal "Management", child_2.title
60
+
61
+ assert_nil parent.parent_id
62
+ assert_equal parent.id, child_1.parent_id
63
+ assert_equal child_1.id, child_2.parent_id
64
+ end
65
+
66
+ test 'should build child page without parent url' do
67
+ options[:pages] << { "title" => "About us", "url" => false, "nodes" => [{ "title" => "Team" }] }
68
+ Seiten::PageCollectionBuilder.call(page_collection, options)
69
+
70
+ assert_equal "About us", page_collection.where(slug: nil).first.title
71
+ assert_equal "Team", page_collection.find_by(slug: 'team').title
72
+ end
73
+
74
+ test 'should build a page that refers to its first child' do
75
+ options[:pages] << { "title" => "About us", "refer" => true, "nodes" => [{ "title" => "Team" }] }
76
+ Seiten::PageCollectionBuilder.call(page_collection, options)
77
+
78
+ assert_equal "/team", page_collection.where(slug: nil).first.refer
79
+ end
80
+
81
+ test 'should build a referer page with an absolute path' do
82
+ options[:pages] << { "title" => "About us", "refer" => "/team" }
83
+ Seiten::PageCollectionBuilder.call(page_collection, options)
84
+
85
+ assert_equal "/team", page_collection.where(slug: nil).first.refer
86
+ end
87
+
88
+ test 'should build a referer page with an external path' do
89
+ options[:pages] << { "title" => "About us", "refer" => "https://codegestalt.com" }
90
+ Seiten::PageCollectionBuilder.call(page_collection, options)
91
+
92
+ assert_equal "https://codegestalt.com", page_collection.where(slug: nil).first.refer
93
+ end
94
+
95
+ test 'should build a page with default layout' do
96
+ options[:pages] << { "title" => "About us" }
97
+ Seiten::PageCollectionBuilder.call(page_collection, options)
98
+
99
+ assert_equal "application", page_collection.find_by(slug: 'about-us').layout
100
+ end
101
+
102
+ test 'should build a page with a layout' do
103
+ options[:pages] << { "title" => "About us", "layout" => "team", "nodes" => [{ "title" => "Team" }] }
104
+ Seiten::PageCollectionBuilder.call(page_collection, options)
105
+
106
+ assert_equal "team", page_collection.find_by(slug: 'about-us').layout
107
+ assert_equal "team", page_collection.find_by(slug: 'about-us/team').layout
108
+ end
109
+
110
+ test 'should build a page with an inherited layout' do
111
+ options[:pages] << { "title" => "About us", "layout" => { "name" => "team", "inherit" => true }, "nodes" => [{ "title" => "Team" }] }
112
+ Seiten::PageCollectionBuilder.call(page_collection, options)
113
+
114
+ assert_equal "team", page_collection.find_by(slug: 'about-us').layout
115
+ assert_equal "team", page_collection.find_by(slug: 'about-us/team').layout
116
+ end
117
+
118
+ test 'should build a page without inheriting the layout' do
119
+ options[:pages] << { "title" => "About us", "layout" => { "name" => "team", "inherit" => false }, "nodes" => [{ "title" => "Team" }] }
120
+ Seiten::PageCollectionBuilder.call(page_collection, options)
121
+
122
+ assert_equal "team", page_collection.find_by(slug: 'about-us').layout
123
+ assert_equal "application", page_collection.find_by(slug: 'about-us/team').layout
124
+ end
125
+
126
+ test 'should raise page refer exception' do
127
+ options[:pages] << { "title" => 'Refer', "refer" => 'home' }
128
+ err = assert_raises Seiten::Errors::PageError do
129
+ Seiten::PageCollectionBuilder.call(page_collection, options)
130
+ end
131
+ assert_match "The `refer` option must be `true` or an absolute or external path", err.message
132
+ end
133
+
134
+ test 'should raise page url exception' do
135
+ options[:pages] << { "title" => 'External path', "url" => 'https://codegestalt.com' }
136
+ err = assert_raises Seiten::Errors::PageError do
137
+ Seiten::PageCollectionBuilder.call(page_collection, options)
138
+ end
139
+ assert_match "The `url` option can not be an external path. Use the `refer` option to link to external resources.", err.message
140
+ end
141
+ end
@@ -0,0 +1,59 @@
1
+ require 'test_helper'
2
+
3
+ class Seiten::PageCollectionTest < ActiveSupport::TestCase
4
+ def navigation
5
+ @navigation ||= Seiten::Navigation.new(name: :test, locale: :en)
6
+ end
7
+
8
+ def page_collection
9
+ @page_collection ||= Seiten::PageCollection.new(
10
+ navigation_id: 'test.en',
11
+ pages: [
12
+ Seiten::Page.new(navigation_id: 'test.en', id: 1),
13
+ Seiten::Page.new(navigation_id: 'test.en', id: 2, parent_id: 1),
14
+ Seiten::Page.new(navigation_id: 'test.en', id: 3, parent_id: 1),
15
+ Seiten::Page.new(navigation_id: 'test.en', id: 4),
16
+ Seiten::Page.new(navigation_id: 'test.en', id: 5)
17
+ ]
18
+ )
19
+ end
20
+
21
+ setup do
22
+ Seiten.navigations << navigation
23
+ end
24
+
25
+ teardown do
26
+ Seiten.navigations = Seiten.navigations - [navigation]
27
+ end
28
+
29
+ test '#navigation' do
30
+ assert_equal navigation, page_collection.navigation
31
+ end
32
+
33
+ test '#all' do
34
+ assert_equal [1,2,3,4,5], page_collection.all.map(&:id)
35
+ end
36
+
37
+ test '#find' do
38
+ assert_equal 3, page_collection.find(3).id
39
+ end
40
+
41
+ test '#find_by' do
42
+ assert_equal 2, page_collection.find_by(id: 2).id
43
+ assert_equal 3, page_collection.find_by(id: 3, parent_id: 1).id
44
+ assert_nil page_collection.find_by(id: 2, parent_id: 2)
45
+ end
46
+
47
+ test '#where' do
48
+ assert_equal [1], page_collection.where(id: 1).map(&:id)
49
+ assert_equal [2,3], page_collection.where(parent_id: 1).map(&:id)
50
+ assert_equal [], page_collection.where(parent_id: 2)
51
+ end
52
+
53
+ test '#new' do
54
+ page = page_collection.new(id: 6)
55
+ assert_equal 6, page.id
56
+ assert_equal 'test.en', page.navigation_id
57
+ assert_equal [1,2,3,4,5,6], page_collection.all.map(&:id)
58
+ end
59
+ end