canonical_link_spec.rb 3.1 KB

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