canonical_link_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Public Knowledge Base canonical link', type: :system, current_user_id: 1, authenticated_as: false do
  4. include_context 'basic Knowledge Base'
  5. let(:path) { '/path' }
  6. let(:subdomain) { 'subdomain.example.net' }
  7. let(:locale) { primary_locale.system_locale.locale }
  8. let(:category_slug) { category.translations.first.to_param }
  9. let(:answer_slug) { published_answer.translations.first.to_param }
  10. before do
  11. published_answer
  12. knowledge_base.update! custom_address: custom_address
  13. end
  14. shared_examples 'having canonical links on all pages' do
  15. it 'includes canonical link on home page' do
  16. visit help_root_path(locale)
  17. expect(page).to have_canonical_url("#{prefix}/#{locale}")
  18. end
  19. it 'includes canonical link on category page' do
  20. visit help_category_path(locale, category)
  21. expect(page).to have_canonical_url("#{prefix}/#{locale}/#{category_slug}")
  22. end
  23. it 'includes canonical link on answer page' do
  24. visit help_answer_path(locale, published_answer.category, published_answer)
  25. expect(page).to have_canonical_url("#{prefix}/#{locale}/#{category_slug}/#{answer_slug}")
  26. end
  27. it 'includes canonical link on tag page' do
  28. visit help_tag_path(locale, published_answer_tag_name)
  29. expect(page).to have_canonical_url("#{prefix}/#{locale}/tag/#{published_answer_tag_name}")
  30. end
  31. end
  32. shared_examples 'core locations' do
  33. let(:scheme) { ssl ? 'https' : 'http' }
  34. before { Setting.set('http_type', scheme) }
  35. context 'with custom domain' do
  36. let(:custom_address) { subdomain }
  37. let(:prefix) { "#{scheme}://#{subdomain}" }
  38. it_behaves_like 'having canonical links on all pages'
  39. end
  40. context 'with custom path' do
  41. let(:custom_address) { path }
  42. let(:prefix) { "#{scheme}://#{Setting.get('fqdn')}#{path}" }
  43. it_behaves_like 'having canonical links on all pages'
  44. end
  45. context 'with custom domain and path' do
  46. let(:custom_address) { "#{subdomain}#{path}" }
  47. let(:prefix) { "#{scheme}://#{subdomain}#{path}" }
  48. it_behaves_like 'having canonical links on all pages'
  49. end
  50. context 'without custom address' do
  51. let(:custom_address) { nil }
  52. let(:prefix) { "#{scheme}://#{Setting.get('fqdn')}/help" }
  53. it_behaves_like 'having canonical links on all pages'
  54. end
  55. end
  56. context 'when SSL disabled' do
  57. let(:ssl) { false }
  58. include_examples 'core locations'
  59. end
  60. context 'when SSL enabled' do
  61. let(:ssl) { true }
  62. include_examples 'core locations'
  63. end
  64. matcher :have_canonical_url do |expected|
  65. match do
  66. return false if canonical_link_element.blank?
  67. canonical_link_target == expected
  68. end
  69. failure_message do
  70. return 'no canonical link found' if canonical_link_element.blank?
  71. "expected canonical link pointing to \"#{expected}\", but found \"#{canonical_link_target}\" instead"
  72. end
  73. def canonical_link_element
  74. return @canonical_link_element if defined?(@canonical_link_element)
  75. @canonical_link_element = actual.first('head link[rel=canonical]', visible: :hidden, minimum: 0)
  76. end
  77. def canonical_link_target
  78. @canonical_link_target ||= canonical_link_element[:href]
  79. end
  80. description { "have canonical tag with href of #{expected}" }
  81. end
  82. end