Project

General

Profile

Defect #40937 » change-href-for-wiki-exports.patch

Liane Hampe, 2024-07-05 11:07

View differences:

app/helpers/application_helper.rb
456 456
      content << "<ul class=\"pages-hierarchy\">\n"
457 457
      pages[node].each do |page|
458 458
        content << "<li>"
459
        if controller.controller_name == 'wiki' && controller.action_name == 'export'
460
          href = "##{page.title}"
461
        else
462
          href = {:controller => 'wiki', :action => 'show',
463
                  :project_id => page.project, :id => page.title, :version => nil}
464
        end
459
        href = if controller.request.format.pdf?
460
                 # absolute link for pdf export of single page or all pages in a single document
461
                 options[:target] = '_blank'
462
                 options[:rel] = 'noopener'
463
                 project_wiki_page_url(project_id: page.project, id: page.title, version: nil)
464
               elsif controller.params[:format] == 'html' && controller.action_name == 'export'
465
                 # relative link for html export of all pages in a single document
466
                 "##{page.title}"
467
               elsif controller.params[:format] == 'html'
468
                 # absolute for html export of single page
469
                 "#{h(page.pretty_title)}.html"
470
               else
471
                 # link handling for rendering pages in UI
472
                 {:controller => 'wiki', :action => 'show',
473
                   :project_id => page.project, :id => page.title, :version => nil}
474
               end
465 475
        content <<
466 476
          link_to(
467 477
            h(page.pretty_title),
468 478
            href,
479
            :target => options[:target],
480
            :rel => options[:rel],
469 481
            :title => (if options[:timestamp] && page.updated_on
470 482
                         l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on))
471 483
                       else
test/helpers/application_helper_test.rb
1816 1816
        distance_of_time_in_words(Time.now, child_page.updated_on)))
1817 1817
  end
1818 1818

  
1819
  def test_render_page_hierarchy_when_action_is_export
1819
  def test_render_page_hierarchy_when_action_is_export_and_format_html
1820 1820
    parent_page = WikiPage.find(1)
1821 1821
    child_page = WikiPage.find_by(parent_id: parent_page.id)
1822 1822
    pages_by_parent_id = {nil => [parent_page], parent_page.id => [child_page]}
1823 1823

  
1824
    # Change controller and action using stub
1825
    controller.stubs(:controller_name).returns('wiki')
1824
    # Change action and format using stub
1825
    controller.stubs(:params).returns({ format: 'html' })
1826 1826
    controller.stubs(:action_name).returns("export")
1827 1827

  
1828 1828
    result = render_page_hierarchy(pages_by_parent_id, nil)
......
1830 1830
    assert_select_in result, 'ul.pages-hierarchy li ul.pages-hierarchy a[href=?]', "##{child_page.title}"
1831 1831
  end
1832 1832

  
1833
  def test_render_page_hierarchy_links_when_format_is_html
1834
    parent_page = WikiPage.find(1)
1835
    child_page = WikiPage.find_by(parent_id: parent_page.id)
1836
    pages_by_parent_id = {nil => [parent_page], parent_page.id => [child_page]}
1837

  
1838
    # Change format using stub
1839
    controller.stubs(:params).returns({ format: 'html' })
1840

  
1841
    result = render_page_hierarchy(pages_by_parent_id, nil)
1842
    assert_select_in result, 'ul.pages-hierarchy li a[href=?]', "#{h(parent_page.pretty_title)}.html"
1843
    assert_select_in result, 'ul.pages-hierarchy li ul.pages-hierarchy a[href=?]', "#{h(child_page.pretty_title)}.html"
1844
  end
1845

  
1846
  def test_render_page_hierarchy_links_when_format_is_pdf
1847
    parent_page = WikiPage.find(1)
1848
    child_page = WikiPage.find_by(parent_id: parent_page.id)
1849
    pages_by_parent_id = {nil => [parent_page], parent_page.id => [child_page]}
1850

  
1851
    # Change request format using stub
1852
    format = mock('format')
1853
    format.stubs(:pdf?).returns(true)
1854

  
1855
    # Stub the controller's request.format to return the mocked format object
1856
    controller.request.stubs(:format).returns(format)
1857

  
1858
    result = render_page_hierarchy(pages_by_parent_id, nil)
1859
    parent_page_url = project_wiki_page_url(project_id: parent_page.project, id: parent_page.title, version: nil)
1860
    child_page_url = project_wiki_page_url(project_id: child_page.project, id: child_page.title, version: nil)
1861

  
1862
    assert_select_in result, 'ul.pages-hierarchy li a[href=?]', parent_page_url
1863
    assert_select_in result, 'ul.pages-hierarchy li ul.pages-hierarchy a[href=?]', child_page_url
1864

  
1865
    # Make sure that the pdf? method returns the expected value
1866
    assert_equal true, controller.request.format.pdf?
1867
  end
1868

  
1833 1869
  def test_link_to_user
1834 1870
    user = User.find(2)
1835 1871
    result = link_to("John Smith", "/users/2", :class => "user active")
(1-1/3)