|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +#-- copyright |
| 4 | +# OpenProject is an open source project management software. |
| 5 | +# Copyright (C) the OpenProject GmbH |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU General Public License version 3. |
| 9 | +# |
| 10 | +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: |
| 11 | +# Copyright (C) 2006-2013 Jean-Philippe Lang |
| 12 | +# Copyright (C) 2010-2013 the ChiliProject Team |
| 13 | +# |
| 14 | +# This program is free software; you can redistribute it and/or |
| 15 | +# modify it under the terms of the GNU General Public License |
| 16 | +# as published by the Free Software Foundation; either version 2 |
| 17 | +# of the License, or (at your option) any later version. |
| 18 | +# |
| 19 | +# This program is distributed in the hope that it will be useful, |
| 20 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | +# GNU General Public License for more details. |
| 23 | +# |
| 24 | +# You should have received a copy of the GNU General Public License |
| 25 | +# along with this program; if not, write to the Free Software |
| 26 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | +# |
| 28 | +# See COPYRIGHT and LICENSE files for more details. |
| 29 | +#++ |
| 30 | + |
| 31 | +require "spec_helper" |
| 32 | + |
| 33 | +RSpec.describe Header::ProjectsHelper do |
| 34 | + # Shorthand for the expected highlight span markup. |
| 35 | + let(:hl) { ->(text) { %(<span class="op-search-highlight">#{text}</span>) } } |
| 36 | + |
| 37 | + describe "#highlight_name" do |
| 38 | + subject(:result) { helper.send(:highlight_name, name, query_terms) } |
| 39 | + |
| 40 | + context "with no query terms" do |
| 41 | + let(:name) { "My Project" } |
| 42 | + let(:query_terms) { [] } |
| 43 | + |
| 44 | + it "returns the plain name" do |
| 45 | + expect(result).to eq "My Project" |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + context "when no term matches" do |
| 50 | + let(:name) { "My Project" } |
| 51 | + let(:query_terms) { ["foo"] } |
| 52 | + |
| 53 | + it "returns the plain name" do |
| 54 | + expect(result).to eq "My Project" |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + context "with a single matching term" do |
| 59 | + let(:name) { "My Project" } |
| 60 | + let(:query_terms) { ["Project"] } |
| 61 | + |
| 62 | + it "wraps the match in a highlight span" do |
| 63 | + expect(result).to eq "My #{hl.call('Project')}" |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + context "with a match at the beginning of the name" do |
| 68 | + let(:name) { "Alpha Team" } |
| 69 | + let(:query_terms) { ["Alpha"] } |
| 70 | + |
| 71 | + it { is_expected.to eq "#{hl.call('Alpha')} Team" } |
| 72 | + end |
| 73 | + |
| 74 | + context "with a match at the end of the name" do |
| 75 | + let(:name) { "Team Alpha" } |
| 76 | + let(:query_terms) { ["Alpha"] } |
| 77 | + |
| 78 | + it { is_expected.to eq "Team #{hl.call('Alpha')}" } |
| 79 | + end |
| 80 | + |
| 81 | + context "when the term matches the full name" do |
| 82 | + let(:name) { "Alpha" } |
| 83 | + let(:query_terms) { ["Alpha"] } |
| 84 | + |
| 85 | + it { is_expected.to eq hl.call("Alpha") } |
| 86 | + end |
| 87 | + |
| 88 | + context "with case-insensitive matching" do |
| 89 | + let(:name) { "My PROJECT" } |
| 90 | + let(:query_terms) { ["project"] } |
| 91 | + |
| 92 | + it "highlights the match using the original casing from the name" do |
| 93 | + expect(result).to eq "My #{hl.call('PROJECT')}" |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + context "with multiple occurrences of the same term" do |
| 98 | + let(:name) { "Foo and Foo" } |
| 99 | + let(:query_terms) { ["Foo"] } |
| 100 | + |
| 101 | + it "highlights every occurrence" do |
| 102 | + expect(result).to eq "#{hl.call('Foo')} and #{hl.call('Foo')}" |
| 103 | + end |
| 104 | + end |
| 105 | + |
| 106 | + context "with multiple non-overlapping terms" do |
| 107 | + let(:name) { "Alpha Beta" } |
| 108 | + let(:query_terms) { ["Alpha", "Beta"] } |
| 109 | + |
| 110 | + it "highlights each term independently" do |
| 111 | + expect(result).to eq "#{hl.call('Alpha')} #{hl.call('Beta')}" |
| 112 | + end |
| 113 | + end |
| 114 | + |
| 115 | + context "with overlapping term matches" do |
| 116 | + # "Over" covers indices 0..4, "erlap" covers 1..6 → merged to 0..6 = "Overlap". |
| 117 | + let(:name) { "Overlap" } |
| 118 | + let(:query_terms) { ["Over", "erlap"] } |
| 119 | + |
| 120 | + it "merges the overlapping ranges into a single span" do |
| 121 | + expect(result).to eq hl.call("Overlap") |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + context "with HTML special characters in the name" do |
| 126 | + let(:name) { "A & B <Project>" } |
| 127 | + let(:query_terms) { ["Project"] } |
| 128 | + |
| 129 | + it "escapes characters outside the match and leaves the span unescaped" do |
| 130 | + expect(result).to eq "A & B <#{hl.call('Project')}>" |
| 131 | + end |
| 132 | + end |
| 133 | + end |
| 134 | +end |
0 commit comments