#-- copyright # OpenProject is an open source project management software. # Copyright (C) 2012-2021 the OpenProject GmbH # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License version 3. # # OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: # Copyright (C) 2006-2013 Jean-Philippe Lang # Copyright (C) 2010-2013 the ChiliProject Team # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # # See COPYRIGHT and LICENSE files for more details. #++ require 'spec_helper' describe ApplicationHelper do include ApplicationHelper include ActionView::Helpers include ActionDispatch::Routing include Rails.application.routes.url_helpers describe ".format_text" do let(:project) { FactoryBot.create :valid_project } let(:identifier) { project.identifier } let(:role) do FactoryBot.create(:role, permissions: %i[ view_work_packages edit_work_packages view_documents browse_repository view_changesets view_wiki_pages ]) end let(:project_member) do FactoryBot.create :user, member_in_project: project, member_through_role: role end let(:document) do FactoryBot.create :document, title: 'Test document', project: project end before do @project = project allow(User).to receive(:current).and_return project_member end after do allow(User).to receive(:current).and_call_original end context "Simple Document links" do let(:document_link) do link_to('Test document', { controller: 'documents', action: 'show', id: document.id }, class: 'document op-uc-link') end context "Plain link" do subject { format_text("document##{document.id}") } it { is_expected.to eq("
#{document_link}
") } end context "Link with document name" do subject { format_text("document##{document.id}") } it { is_expected.to eq("#{document_link}
") } end context "Escaping plain link" do subject { format_text("!document##{document.id}") } it { is_expected.to eq("document##{document.id}
") } end context "Escaping link with document name" do subject { format_text('!document:"Test document"') } it { is_expected.to eq('document:"Test document"
') } end end context 'Cross-Project Document Links' do let(:the_other_project) { FactoryBot.create :valid_project } context "By name without project" do subject { format_text("document:\"#{document.title}\"", project: the_other_project) } it { is_expected.to eq('document:"Test document"
') } end context "By id and given project" do subject { format_text("#{identifier}:document##{document.id}", project: the_other_project) } it { is_expected.to eq("") } end context "By name and given project" do subject { format_text("#{identifier}:document:\"#{document.title}\"", project: the_other_project) } it { is_expected.to eq("") } end context "Invalid link" do subject { format_text("invalid:document:\"Test document\"", project: the_other_project) } it { is_expected.to eq('invalid:document:"Test document"
') } end end end end