2025-05-05 09:29:55 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2013-12-11 13:42:27 +01:00
|
|
|
#-- copyright
|
2020-01-15 11:31:26 +01:00
|
|
|
# OpenProject is an open source project management software.
|
2024-07-30 13:42:36 +02:00
|
|
|
# Copyright (C) the OpenProject GmbH
|
2013-12-11 13:42:27 +01:00
|
|
|
#
|
|
|
|
|
# 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:
|
2021-01-13 17:47:45 +01:00
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
2013-12-11 13:42:27 +01:00
|
|
|
# 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.
|
|
|
|
|
#
|
2021-09-02 21:49:06 +02:00
|
|
|
# See COPYRIGHT and LICENSE files for more details.
|
2013-12-11 13:42:27 +01:00
|
|
|
#++
|
|
|
|
|
|
|
|
|
|
require "spec_helper"
|
|
|
|
|
|
2023-05-31 12:15:15 +02:00
|
|
|
RSpec.describe SearchController do
|
2020-03-27 16:14:21 +01:00
|
|
|
shared_let(:project) do
|
2022-01-24 19:22:35 +01:00
|
|
|
create(:project,
|
2022-02-02 21:48:06 +01:00
|
|
|
name: "eCookbook")
|
2019-01-10 17:11:59 +01:00
|
|
|
end
|
|
|
|
|
|
2020-03-27 16:14:21 +01:00
|
|
|
shared_let(:other_project) do
|
2022-01-24 19:22:35 +01:00
|
|
|
create(:project,
|
2022-02-02 21:48:06 +01:00
|
|
|
name: "Other project")
|
2019-01-10 17:11:59 +01:00
|
|
|
end
|
|
|
|
|
|
2020-03-27 16:14:21 +01:00
|
|
|
shared_let(:subproject) do
|
2022-01-24 19:22:35 +01:00
|
|
|
create(:project,
|
2022-02-02 21:48:06 +01:00
|
|
|
name: "Child project",
|
|
|
|
|
parent: project)
|
2019-01-10 17:11:59 +01:00
|
|
|
end
|
|
|
|
|
|
2020-03-27 16:14:21 +01:00
|
|
|
shared_let(:role) do
|
2023-10-05 15:28:31 +02:00
|
|
|
create(:project_role, permissions: %i[view_wiki_pages view_work_packages])
|
2019-01-10 17:11:59 +01:00
|
|
|
end
|
|
|
|
|
|
2020-03-27 16:14:21 +01:00
|
|
|
shared_let(:user) do
|
2023-09-27 17:53:04 +02:00
|
|
|
create(:user, member_with_roles: { project => role, subproject => role })
|
2019-01-10 17:11:59 +01:00
|
|
|
end
|
|
|
|
|
|
2020-03-27 16:14:21 +01:00
|
|
|
shared_let(:wiki_page) do
|
2022-01-24 19:22:35 +01:00
|
|
|
create(:wiki_page,
|
2022-02-02 21:48:06 +01:00
|
|
|
title: "How to solve an issue",
|
|
|
|
|
wiki: project.wiki)
|
2019-01-10 17:11:59 +01:00
|
|
|
end
|
|
|
|
|
|
2020-03-27 16:14:21 +01:00
|
|
|
shared_let(:work_package_1) do
|
2022-01-24 19:22:35 +01:00
|
|
|
create(:work_package,
|
2022-02-02 21:48:06 +01:00
|
|
|
subject: "This is a test issue",
|
|
|
|
|
project:)
|
2019-01-10 17:11:59 +01:00
|
|
|
end
|
|
|
|
|
|
2020-03-27 16:14:21 +01:00
|
|
|
shared_let(:work_package_2) do
|
2022-01-24 19:22:35 +01:00
|
|
|
create(:work_package,
|
2022-02-02 21:48:06 +01:00
|
|
|
subject: "Issue test 2",
|
|
|
|
|
project:,
|
|
|
|
|
status: create(:closed_status))
|
2019-01-10 17:11:59 +01:00
|
|
|
end
|
|
|
|
|
|
2020-03-27 16:14:21 +01:00
|
|
|
shared_let(:work_package_3) do
|
2022-01-24 19:22:35 +01:00
|
|
|
create(:work_package,
|
2022-02-02 21:48:06 +01:00
|
|
|
subject: "Issue test 3",
|
|
|
|
|
project: subproject)
|
2019-01-10 17:11:59 +01:00
|
|
|
end
|
|
|
|
|
|
2020-03-27 16:14:21 +01:00
|
|
|
shared_let(:work_package_4) do
|
2022-01-24 19:22:35 +01:00
|
|
|
create(:work_package,
|
2022-02-02 21:48:06 +01:00
|
|
|
subject: "Issue test 4",
|
|
|
|
|
project: other_project)
|
2019-01-10 17:11:59 +01:00
|
|
|
end
|
2013-12-11 13:42:27 +01:00
|
|
|
|
|
|
|
|
shared_examples_for "successful search" do
|
2018-11-23 15:06:32 +01:00
|
|
|
it { expect(response).to be_successful }
|
2013-12-11 13:42:27 +01:00
|
|
|
it { expect(response).to render_template("index") }
|
|
|
|
|
end
|
|
|
|
|
|
2019-01-10 17:11:59 +01:00
|
|
|
before { allow(User).to receive(:current).and_return user }
|
2013-12-11 13:42:27 +01:00
|
|
|
|
|
|
|
|
describe "project search" do
|
2016-09-13 14:40:10 +02:00
|
|
|
context "without a search parameter" do
|
2019-01-10 17:11:59 +01:00
|
|
|
before { get :index }
|
2013-12-11 13:42:27 +01:00
|
|
|
|
2016-09-13 14:40:10 +02:00
|
|
|
it_behaves_like "successful search"
|
|
|
|
|
end
|
2013-12-11 13:42:27 +01:00
|
|
|
|
|
|
|
|
context "search parameter" do
|
2016-09-13 14:40:10 +02:00
|
|
|
context "is a search string" do
|
|
|
|
|
before do
|
|
|
|
|
get :index, params: { q: "cook" }
|
|
|
|
|
end
|
2013-12-11 13:42:27 +01:00
|
|
|
|
2016-09-13 14:40:10 +02:00
|
|
|
it_behaves_like "successful search"
|
|
|
|
|
end
|
2013-12-11 13:42:27 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2013-12-18 18:03:46 +01:00
|
|
|
describe "scoped project search" do
|
2019-01-10 17:11:59 +01:00
|
|
|
before { get :index, params: { project_id: project.id } }
|
2013-12-18 18:03:46 +01:00
|
|
|
|
|
|
|
|
it_behaves_like "successful search"
|
|
|
|
|
|
2014-11-03 23:43:33 +01:00
|
|
|
it { expect(assigns(:project).id).to be(project.id) }
|
2013-12-18 18:03:46 +01:00
|
|
|
end
|
|
|
|
|
|
2019-01-10 17:11:59 +01:00
|
|
|
describe "searching in all modules" do
|
|
|
|
|
context "when searching in all projects" do
|
|
|
|
|
before { get :index, params: { q: "issue", scope: "all" } }
|
2013-12-11 13:42:27 +01:00
|
|
|
|
2016-09-13 14:40:10 +02:00
|
|
|
it_behaves_like "successful search"
|
2013-12-11 13:42:27 +01:00
|
|
|
|
2016-09-13 14:40:10 +02:00
|
|
|
describe "#result" do
|
2019-01-10 17:11:59 +01:00
|
|
|
it { expect(assigns(:results).count).to be(4) }
|
2016-09-13 14:40:10 +02:00
|
|
|
it { expect(assigns(:results)).to include(work_package_1) }
|
|
|
|
|
it { expect(assigns(:results)).to include(work_package_2) }
|
2019-01-10 17:11:59 +01:00
|
|
|
it { expect(assigns(:results)).to include(work_package_3) }
|
|
|
|
|
it { expect(assigns(:results)).to include(wiki_page) }
|
|
|
|
|
it { expect(assigns(:results)).not_to include(work_package_4) }
|
|
|
|
|
end
|
2013-12-11 13:42:27 +01:00
|
|
|
|
2019-03-13 13:31:01 +01:00
|
|
|
describe "#results_count" do
|
|
|
|
|
it { expect(assigns(:results_count)).to be_a(Hash) }
|
|
|
|
|
it { expect(assigns(:results_count)["work_packages"]).to be(3) }
|
2019-01-10 17:11:59 +01:00
|
|
|
end
|
2013-12-11 13:42:27 +01:00
|
|
|
end
|
2014-03-25 20:51:52 +02:00
|
|
|
|
2023-11-14 10:25:00 +01:00
|
|
|
context "when searching in all projects with an untransliterable character" do
|
|
|
|
|
before do
|
|
|
|
|
work_package_1.update_column(:subject, "Something 会议 something")
|
|
|
|
|
get :index, params: { q: "会议", scope: "all" }
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it_behaves_like "successful search"
|
|
|
|
|
|
|
|
|
|
it "returns the result", :aggregate_failures do
|
|
|
|
|
expect(assigns(:results).count).to be(1)
|
|
|
|
|
expect(assigns(:results)).to include(work_package_1)
|
|
|
|
|
expect(assigns(:results_count)).to be_a(Hash)
|
|
|
|
|
expect(assigns(:results_count)["work_packages"]).to be(1)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-01-10 17:11:59 +01:00
|
|
|
context "when searching in project and its subprojects" do
|
|
|
|
|
before { get :index, params: { q: "issue", project_id: project.id, scope: "subprojects" } }
|
|
|
|
|
|
|
|
|
|
it_behaves_like "successful search"
|
|
|
|
|
|
|
|
|
|
describe "#result" do
|
|
|
|
|
it { expect(assigns(:results).count).to be(4) }
|
|
|
|
|
it { expect(assigns(:results)).to include(work_package_1) }
|
|
|
|
|
it { expect(assigns(:results)).to include(work_package_2) }
|
|
|
|
|
it { expect(assigns(:results)).to include(work_package_3) }
|
|
|
|
|
it { expect(assigns(:results)).to include(wiki_page) }
|
|
|
|
|
it { expect(assigns(:results)).not_to include(work_package_4) }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when searching in project without its subprojects" do
|
|
|
|
|
before { get :index, params: { q: "issue", project_id: project.id, scope: "current_project" } }
|
|
|
|
|
|
|
|
|
|
it_behaves_like "successful search"
|
|
|
|
|
|
|
|
|
|
describe "#result" do
|
|
|
|
|
it { expect(assigns(:results).count).to be(3) }
|
|
|
|
|
it { expect(assigns(:results)).to include(work_package_1) }
|
|
|
|
|
it { expect(assigns(:results)).to include(work_package_2) }
|
|
|
|
|
it { expect(assigns(:results)).to include(wiki_page) }
|
|
|
|
|
it { expect(assigns(:results)).not_to include(work_package_3) }
|
|
|
|
|
it { expect(assigns(:results)).not_to include(work_package_4) }
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-09-13 14:40:10 +02:00
|
|
|
context "when searching for a note" do
|
2019-01-10 17:11:59 +01:00
|
|
|
let!(:note_1) do
|
2023-03-07 15:04:32 +01:00
|
|
|
create(:work_package_journal,
|
2022-02-02 21:48:06 +01:00
|
|
|
journable_id: work_package_1.id,
|
|
|
|
|
notes: "Test note 1",
|
2023-03-07 15:04:32 +01:00
|
|
|
version: 2)
|
2019-01-10 17:11:59 +01:00
|
|
|
end
|
|
|
|
|
let!(:note_2) do
|
2023-03-07 15:04:32 +01:00
|
|
|
create(:work_package_journal,
|
2022-02-02 21:48:06 +01:00
|
|
|
journable_id: work_package_1.id,
|
|
|
|
|
notes: "Special note 2",
|
2023-03-07 15:04:32 +01:00
|
|
|
version: 3)
|
2019-01-10 17:11:59 +01:00
|
|
|
end
|
2014-03-25 20:51:52 +02:00
|
|
|
|
2016-09-13 14:40:10 +02:00
|
|
|
before do
|
2021-02-11 16:02:18 +01:00
|
|
|
get :index, params: { q: "note" }
|
2022-05-31 11:55:27 +02:00
|
|
|
end
|
|
|
|
|
|
2016-09-13 14:40:10 +02:00
|
|
|
describe "second note predecessor" do
|
|
|
|
|
subject { note_2.send :predecessor }
|
2014-03-25 20:51:52 +02:00
|
|
|
|
2016-09-13 14:40:10 +02:00
|
|
|
it { is_expected.to eq note_1 }
|
|
|
|
|
it { expect(note_1.data).not_to be_nil }
|
|
|
|
|
it { expect(subject.data).not_to be_nil }
|
|
|
|
|
end
|
2014-03-25 20:51:52 +02:00
|
|
|
|
2016-09-13 14:40:10 +02:00
|
|
|
it_behaves_like "successful search"
|
2014-03-25 20:51:52 +02:00
|
|
|
|
2016-09-13 14:40:10 +02:00
|
|
|
describe "#result" do
|
|
|
|
|
it { expect(assigns(:results).count).to be 1 }
|
2014-03-25 20:51:52 +02:00
|
|
|
|
2016-09-13 14:40:10 +02:00
|
|
|
it { expect(assigns(:results)).to include work_package_1 }
|
2014-03-25 20:51:52 +02:00
|
|
|
|
2023-05-02 19:16:28 +03:00
|
|
|
it { expect(assigns(:tokens)).to include "note" }
|
2014-03-25 20:51:52 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "helper methods" do
|
|
|
|
|
describe "#scan_query_tokens" do
|
|
|
|
|
subject { @controller.send(:scan_query_tokens, query) }
|
|
|
|
|
|
|
|
|
|
context "with one token" do
|
|
|
|
|
let(:query) { "word" }
|
|
|
|
|
|
2014-04-17 23:35:32 +02:00
|
|
|
it { is_expected.to eq %w(word) }
|
2014-03-25 20:51:52 +02:00
|
|
|
|
|
|
|
|
context "with double quotes" do
|
|
|
|
|
let(:query) { '"hello world"' }
|
|
|
|
|
|
2014-04-17 23:35:32 +02:00
|
|
|
it { is_expected.to eq ["hello world"] }
|
2014-03-25 20:51:52 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "with multiple tokens" do
|
|
|
|
|
let(:query) { "hello world something-hyphenated" }
|
|
|
|
|
|
2014-04-17 23:35:32 +02:00
|
|
|
it { is_expected.to eq %w(hello world something-hyphenated) }
|
2014-03-25 20:51:52 +02:00
|
|
|
|
|
|
|
|
context "with double quotes" do
|
|
|
|
|
let(:query) { 'hello "fallen world" something-hyphenated' }
|
|
|
|
|
|
2014-04-17 23:35:32 +02:00
|
|
|
it { is_expected.to eq ["hello", "fallen world", "something-hyphenated"] }
|
2014-03-25 20:51:52 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2013-12-11 13:42:27 +01:00
|
|
|
end
|
|
|
|
|
end
|