OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Stage and Gate specific filter
ulfertsbd7846f8228d
diff --git a/app/components/projects/projects_filters_component.rb b/app/components/projects/projects_filters_component.rbindex 8f107d34561..8f54ad4c79d 100644--- a/app/components/projects/projects_filters_component.rb+++ b/app/components/projects/projects_filters_component.rb@@ -47,17 +47,19 @@ class Projects::ProjectsFiltersComponent < Filter::FilterComponentallowlist = [Queries::Filters::Shared::CustomFields::Base,Queries::Projects::Filters::ActiveFilter,+ Queries::Projects::Filters::AnyStageOrGateFilter,Queries::Projects::Filters::CreatedAtFilter,Queries::Projects::Filters::FavoredFilter,Queries::Projects::Filters::IdFilter,Queries::Projects::Filters::LatestActivityAtFilter,+ Queries::Projects::Filters::LifeCycleGateFilter,+ Queries::Projects::Filters::LifeCycleStageFilter,Queries::Projects::Filters::MemberOfFilter,Queries::Projects::Filters::NameAndIdentifierFilter,Queries::Projects::Filters::ProjectStatusFilter,Queries::Projects::Filters::PublicFilter,Queries::Projects::Filters::TemplatedFilter,- Queries::Projects::Filters::TypeFilter,- Queries::Projects::Filters::AnyStageOrGateFilter+ Queries::Projects::Filters::TypeFilter]allowlist.any? { |clazz| filter.is_a? clazz }diff --git a/app/models/queries/projects.rb b/app/models/queries/projects.rbindex 2f0bf4e2b51..374797dac82 100644--- a/app/models/queries/projects.rb+++ b/app/models/queries/projects.rb@@ -28,45 +28,47 @@module Queries::Projects::Queries::Register.register(ProjectQuery) do+ filter Filters::ActiveFilterfilter Filters::AncestorFilter- filter Filters::AvailableProjectAttributesFilter+ filter Filters::AnyStageOrGateFilterfilter Filters::AvailableCustomFieldsProjectsFilter- filter Filters::TypeFilter- filter Filters::ActiveFilter- filter Filters::TemplatedFilter- filter Filters::PublicFilter- filter Filters::NameFilter- filter Filters::NameAndIdentifierFilter- filter Filters::MemberOfFilter- filter Filters::TypeaheadFilter- filter Filters::CustomFieldFilter+ filter Filters::AvailableProjectAttributesFilterfilter Filters::CreatedAtFilter+ filter Filters::CustomFieldFilter+ filter Filters::FavoredFilter+ filter Filters::IdFilterfilter Filters::LatestActivityAtFilter- filter Filters::PrincipalFilter+ filter Filters::LifeCycleGateFilter+ filter Filters::LifeCycleStageFilter+ filter Filters::MemberOfFilter+ filter Filters::NameAndIdentifierFilter+ filter Filters::NameFilterfilter Filters::ParentFilter- filter Filters::IdFilter+ filter Filters::PrincipalFilterfilter Filters::ProjectStatusFilter+ filter Filters::PublicFilter+ filter Filters::TemplatedFilter+ filter Filters::TypeFilter+ filter Filters::TypeaheadFilterfilter Filters::UserActionFilterfilter Filters::VisibleFilter- filter Filters::FavoredFilter- filter Filters::AnyStageOrGateFilter+ order Orders::CustomFieldOrderorder Orders::DefaultOrderorder Orders::LatestActivityAtOrder- order Orders::RequiredDiskSpaceOrder- order Orders::CustomFieldOrderorder Orders::LifeCycleStepOrder- order Orders::ProjectStatusOrderorder Orders::NameOrder+ order Orders::ProjectStatusOrder+ order Orders::RequiredDiskSpaceOrderorder Orders::TypeaheadOrderselect Selects::CreatedAtselect Selects::CustomFieldselect Selects::Default+ select Selects::Favoredselect Selects::LatestActivityAtselect Selects::LifeCycleStepselect Selects::RequiredDiskSpaceselect Selects::Status- select Selects::Favoredendenddiff --git a/app/models/queries/projects/filters/any_stage_or_gate_filter.rb b/app/models/queries/projects/filters/any_stage_or_gate_filter.rbindex 95c3afe0fd5..574c38d8d18 100644--- a/app/models/queries/projects/filters/any_stage_or_gate_filter.rb+++ b/app/models/queries/projects/filters/any_stage_or_gate_filter.rb@@ -29,49 +29,13 @@# ++class Queries::Projects::Filters::AnyStageOrGateFilter < Queries::Projects::Filters::Base+ include Queries::Projects::Filters::FilterOnLifeCycleinclude Queries::Operators::DateRangeClauses- def type- :date- end-- def available_operators- [- ::Queries::Operators::Today,- ::Queries::Operators::ThisWeek,- ::Queries::Operators::OnDate,- ::Queries::Operators::BetweenDate- ]- end-- def available?- OpenProject::FeatureDecisions.stages_and_gates_active? &&- User.current.allowed_in_any_project?(:view_project_stages_and_gates)- end-def human_nameI18n.t("project.filters.any_stage_or_gate")end- def where- scope = case operator.to_sym- when Queries::Operators::OnDate.to_sym- on_date- when Queries::Operators::Today.to_sym- on_today- when Queries::Operators::BetweenDate.to_sym- between_date- when Queries::Operators::ThisWeek.to_sym- this_week- else- raise "Unknown operator #{operator}"- end-- scope- .arel- .exists- end-def on_datestage_where_on(parsed_start).or(gate_where(parsed_end))@@ -91,67 +55,4 @@ class Queries::Projects::Filters::AnyStageOrGateFilter < Queries::Projects::Filtstage_overlaps_this_week.or(gate_where(beginning_of_week.to_date, end_of_week.to_date))end-- def stage_where_on(start_date, end_date = start_date)- life_cycle_scope(Project::Stage.name)- .where(date_range_clause(Project::LifeCycleStep.table_name, "start_date", nil, start_date))- .where(date_range_clause(Project::LifeCycleStep.table_name, "end_date", end_date, nil))- end-- def stage_where_between(start_date, end_date)- life_cycle_scope(Project::Stage.name)- .where(date_range_clause(Project::LifeCycleStep.table_name, "start_date", start_date, nil))- .where(date_range_clause(Project::LifeCycleStep.table_name, "end_date", nil, end_date))- end-- def gate_where(start_date, end_date = start_date)- # On gates, only the start_date is set.- life_cycle_scope(Project::Gate.name)- .where(date_range_clause(Project::LifeCycleStep.table_name, "start_date", start_date, end_date))- end-- def stage_overlaps_this_week- life_cycle_scope(Project::Stage.name)- .where.not(start_date: nil)- .where.not(end_date: nil)- .where(- <<~SQL.squish, beginning_of_week, end_of_week- daterange(#{Project::LifeCycleStep.table_name}.start_date,- #{Project::LifeCycleStep.table_name}.end_date,- '[]')- &&- daterange(?, ?, '[]')- SQL- )- end-- def life_cycle_scope(type)- Project::LifeCycleStep- .where("#{Project::LifeCycleStep.table_name}.project_id = #{Project.table_name}.id")- .where(project_id: Project.allowed_to(User.current, :view_project_stages_and_gates))- .where(type:)- .active- end-- def parsed_start- values.first.present? ? Date.parse(values.first) : nil- end-- def parsed_end- values.last.present? ? Date.parse(values.last) : nil- end-- def today- Time.zone.today- end-- def beginning_of_week- OpenProject::Internationalization::Date.time_at_beginning_of_week- end-- def end_of_week- beginning_of_week + 7.days- end-- delegate :connection, to: :"ActiveRecord::Base"enddiff --git a/app/models/queries/projects/filters/dynamically_from_life_cycle.rb b/app/models/queries/projects/filters/dynamically_from_life_cycle.rbnew file mode 100644index 00000000000..9479ed1d6f3--- /dev/null+++ b/app/models/queries/projects/filters/dynamically_from_life_cycle.rb@@ -0,0 +1,93 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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.+# ++++module Queries::Projects::Filters::DynamicallyFromLifeCycle+ extend ActiveSupport::Concern++ included do+ def initialize(name, options = {})+ @life_cycle_step_definition = options[:life_cycle_step_definition]++ super+ end++ private++ attr_accessor :life_cycle_step_definition+ end++ class_methods do+ def all_for(context = nil)+ all_step_definitions+ .map do |step|+ create!(name: name_for_step(step), context:)+ rescue ::Queries::Filters::InvalidError+ Rails.logger.error "Failed to map life cycle step definition filter for #{step.name} (CF##{step.id})."+ nil+ end+ end++ def create!(name:, **options)+ life_cycle_step_definition = find_by_accessor(name)+ raise ::Queries::Filters::InvalidError if life_cycle_step_definition.nil?++ new(name, options.merge(life_cycle_step_definition:))+ end++ def key+ raise NotImplementedError+ end++ def all_step_definitions+ key = %w[Queries::Projects::Filters::LifeCycleStepFilter all_step_definitions]++ RequestStore+ .fetch(key) { Project::LifeCycleStepDefinition.all.to_a }+ .select { |lcsd| lcsd.is_a?(step_subclass) }+ end++ def find_by_accessor(name)+ match = name.match key++ if match.present? && match[1].to_i > 0+ all_step_definitions+ .detect { |lcsd| lcsd.id == match[1].to_i }+ end+ end++ def name_for_step(_step)+ raise NotImplementedError+ end++ def step_subclass+ raise NotImplementedError+ end+ end+enddiff --git a/app/models/queries/projects/filters/filter_on_life_cycle.rb b/app/models/queries/projects/filters/filter_on_life_cycle.rbnew file mode 100644index 00000000000..994ffcc5e46--- /dev/null+++ b/app/models/queries/projects/filters/filter_on_life_cycle.rb@@ -0,0 +1,158 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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.+# ++++module Queries::Projects::Filters::FilterOnLifeCycle+ extend ActiveSupport::Concern+ include Queries::Operators::DateRangeClauses++ def type+ :date+ end++ def available_operators+ [+ ::Queries::Operators::Today,+ ::Queries::Operators::ThisWeek,+ ::Queries::Operators::OnDate,+ ::Queries::Operators::BetweenDate+ ]+ end++ def available?+ OpenProject::FeatureDecisions.stages_and_gates_active? &&+ User.current.allowed_in_any_project?(:view_project_stages_and_gates)+ end++ def where+ scope = case operator.to_sym+ when Queries::Operators::OnDate.to_sym+ on_date+ when Queries::Operators::Today.to_sym+ on_today+ when Queries::Operators::BetweenDate.to_sym+ between_date+ when Queries::Operators::ThisWeek.to_sym+ this_week+ else+ raise "Unknown operator #{operator}"+ end++ scope+ .arel+ .exists+ end++ private++ def on_date+ raise NotImplementedError+ end++ def on_today+ raise NotImplementedError+ end++ def between_date+ raise NotImplementedError+ end++ def this_week+ raise NotImplementedError+ end++ def life_cycle_scope_limit(scope)+ scope+ end++ def stage_where_on(start_date, end_date = start_date)+ life_cycle_scope(Project::Stage.name)+ .where(date_range_clause(Project::LifeCycleStep.table_name, "start_date", nil, start_date))+ .where(date_range_clause(Project::LifeCycleStep.table_name, "end_date", end_date, nil))+ end++ def stage_where_between(start_date, end_date)+ life_cycle_scope(Project::Stage.name)+ .where(date_range_clause(Project::LifeCycleStep.table_name, "start_date", start_date, nil))+ .where(date_range_clause(Project::LifeCycleStep.table_name, "end_date", nil, end_date))+ end++ def stage_overlaps_this_week+ life_cycle_scope(Project::Stage.name)+ .where.not(start_date: nil)+ .where.not(end_date: nil)+ .where(+ <<~SQL.squish, beginning_of_week, end_of_week+ daterange(#{Project::LifeCycleStep.table_name}.start_date,+ #{Project::LifeCycleStep.table_name}.end_date,+ '[]')+ &&+ daterange(?, ?, '[]')+ SQL+ )+ end++ def gate_where(start_date, end_date = start_date)+ # On gates, only the start_date is set.+ life_cycle_scope(Project::Gate.name)+ .where(date_range_clause(Project::LifeCycleStep.table_name, "start_date", start_date, end_date))+ end++ def parsed_start+ values.first.present? ? Date.parse(values.first) : nil+ end++ def parsed_end+ values.last.present? ? Date.parse(values.last) : nil+ end++ def today+ Time.zone.today+ end++ def beginning_of_week+ OpenProject::Internationalization::Date.time_at_beginning_of_week+ end++ def end_of_week+ beginning_of_week + 7.days+ end++ def life_cycle_scope(type)+ life_cycle_scope = Project::LifeCycleStep+ .where("#{Project::LifeCycleStep.table_name}.project_id = #{Project.table_name}.id")+ .where(project_id: Project.allowed_to(User.current, :view_project_stages_and_gates))+ .where(type:)+ .active++ life_cycle_scope_limit(life_cycle_scope)+ end++ delegate :connection, to: :"ActiveRecord::Base"+enddiff --git a/app/models/queries/projects/filters/life_cycle_gate_filter.rb b/app/models/queries/projects/filters/life_cycle_gate_filter.rbnew file mode 100644index 00000000000..7c3fdd4f0b0--- /dev/null+++ b/app/models/queries/projects/filters/life_cycle_gate_filter.rb@@ -0,0 +1,77 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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.+# ++++class Queries::Projects::Filters::LifeCycleGateFilter < Queries::Projects::Filters::Base+ include Queries::Projects::Filters::DynamicallyFromLifeCycle+ include Queries::Projects::Filters::FilterOnLifeCycle++ class << self+ def key+ /\Alcsd_gate_(\d+)\z/+ end++ private++ def name_for_step(stage)+ "lcsd_gate_#{stage.id}"+ end++ def step_subclass+ Project::GateDefinition+ end+ end++ def human_name+ I18n.t("project.filters.life_cycle_gate", gate: life_cycle_step_definition.name)+ end++ private++ def on_date+ gate_where(parsed_end)+ end++ def on_today+ gate_where(today, today)+ end++ def between_date+ gate_where(parsed_start, parsed_end)+ end++ def this_week+ gate_where(beginning_of_week.to_date, end_of_week.to_date)+ end++ def life_cycle_scope_limit(scope)+ super+ .where(definition_id: life_cycle_step_definition.id)+ end+enddiff --git a/app/models/queries/projects/filters/life_cycle_stage_filter.rb b/app/models/queries/projects/filters/life_cycle_stage_filter.rbnew file mode 100644index 00000000000..34e38d6958a--- /dev/null+++ b/app/models/queries/projects/filters/life_cycle_stage_filter.rb@@ -0,0 +1,77 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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.+# ++++class Queries::Projects::Filters::LifeCycleStageFilter < Queries::Projects::Filters::Base+ include Queries::Projects::Filters::DynamicallyFromLifeCycle+ include Queries::Projects::Filters::FilterOnLifeCycle++ class << self+ def key+ /\Alcsd_stage_(\d+)\z/+ end++ private++ def name_for_step(stage)+ "lcsd_stage_#{stage.id}"+ end++ def step_subclass+ Project::StageDefinition+ end+ end++ def human_name+ I18n.t("project.filters.life_cycle_stage", stage: life_cycle_step_definition.name)+ end++ private++ def on_date+ stage_where_on(parsed_start)+ end++ def on_today+ stage_where_on(today)+ end++ def between_date+ stage_where_between(parsed_start, parsed_end)+ end++ def this_week+ stage_overlaps_this_week+ end++ def life_cycle_scope_limit(scope)+ super+ .where(definition_id: life_cycle_step_definition.id)+ end+enddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex 1d8ae6b842e..52b68b849ef 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -3490,6 +3490,8 @@ en:title: "Delete the project %{name}"filters:any_stage_or_gate: "Any stage or gate"+ life_cycle_stage: "Lifecycle stage: %{stage}"+ life_cycle_gate: "Lifecycle gate: %{gate}"identifier:warning_one: Members of the project will have to relocate the project's repositories.warning_two: Existing links to the project will no longer work.diff --git a/spec/features/projects/lists/filters_spec.rb b/spec/features/projects/lists/filters_spec.rbindex aba50a8be56..7c2a20e2253 100644--- a/spec/features/projects/lists/filters_spec.rb+++ b/spec/features/projects/lists/filters_spec.rb@@ -617,14 +617,14 @@ RSpec.describe "Projects list filters", :js, with_settings: { login_required?: fendend- describe "filtering for lifecycle" do+ describe "filtering for any lifecycle step" doshared_let(:stage) docreate(:project_stage, project:, start_date: Time.zone.today - 5.days, end_date: Time.zone.today + 10.days)endshared_let(:gate) { create(:project_gate, project: public_project, date: Time.zone.today + 8.days) }context "with the feature flag disabled", with_flag: { stages_and_gates: false } do- it "does not have any lifecycle filter" do+ it "does not have the lifecycle step (any) filter" doload_and_open_filters managerprojects_page.expect_filter_not_available("Any stage or gate")@@ -675,7 +675,7 @@ RSpec.describe "Projects list filters", :js, with_settings: { login_required?: fendcontext "without the necessary permissions" do- it "does not have any lifecycle filter" do+ it "does not have the lifecycle step (any) filter" doload_and_open_filters managerprojects_page.expect_filter_not_available("Any stage or gate")@@ -683,4 +683,154 @@ RSpec.describe "Projects list filters", :js, with_settings: { login_required?: fendendend++ describe "filtering for a specific lifecycle stage" do+ shared_let(:stage) do+ create(:project_stage, project:, start_date: Time.zone.today - 5.days, end_date: Time.zone.today + 10.days)+ end++ context "with the feature flag disabled", with_flag: { stages_and_gates: false } do+ it "does not have the lifecycle (specific stage) filter" do+ load_and_open_filters manager++ projects_page.expect_filter_not_available("Lifecycle stage: #{stage.name}")+ end+ end++ context "with the feature flag enabled", with_flag: { stages_and_gates: true } do+ context "with the necessary permissions" do+ before do+ project_role.add_permission!(:view_project_stages_and_gates)+ end++ it "allows filtering the projects by the life cycle stage" do+ load_and_open_filters manager++ projects_page.set_filter("lcsd_stage_#{stage.id}",+ "Lifecycle stage: #{stage.name}",+ "on",+ [Time.zone.today + 5.days])++ projects_page.expect_projects_not_listed(development_project, public_project)+ projects_page.expect_projects_in_order(project)++ projects_page.remove_filter("lcsd_stage_#{stage.id}")++ projects_page.expect_projects_in_order(development_project, project, public_project)++ projects_page.set_filter("lcsd_stage_#{stage.id}",+ "Lifecycle stage: #{stage.name}",+ "today")++ projects_page.expect_projects_not_listed(development_project, public_project)+ projects_page.expect_projects_in_order(project)++ projects_page.remove_filter("lcsd_stage_#{stage.id}")++ projects_page.expect_projects_in_order(development_project, project, public_project)++ projects_page.set_filter("lcsd_stage_#{stage.id}",+ "Lifecycle stage: #{stage.name}",+ "between",+ [Time.zone.today - 5.days, Time.zone.today + 10.days])++ projects_page.expect_projects_not_listed(development_project, public_project)+ projects_page.expect_projects_in_order(project)++ projects_page.remove_filter("lcsd_stage_#{stage.id}")++ projects_page.expect_projects_in_order(development_project, project, public_project)++ projects_page.set_filter("lcsd_stage_#{stage.id}",+ "Lifecycle stage: #{stage.name}",+ "this week")++ projects_page.expect_projects_not_listed(development_project, public_project)+ projects_page.expect_projects_in_order(project)+ end+ end++ context "without the necessary permissions" do+ it "does not have the lifecycle (specific stage) filter" do+ load_and_open_filters manager++ projects_page.expect_filter_not_available("Lifecycle stage: #{stage.name}")+ end+ end+ end++ describe "filtering for a specific lifecycle gate" do+ shared_let(:gate) { create(:project_gate, project: project, date: Time.zone.today) }++ context "with the feature flag disabled", with_flag: { stages_and_gates: false } do+ it "does not have the lifecycle (specific gate) filter" do+ load_and_open_filters manager++ projects_page.expect_filter_not_available("Lifecycle gate: #{gate.name}")+ end+ end++ context "with the feature flag enabled", with_flag: { stages_and_gates: true } do+ context "with the necessary permissions" do+ before do+ project_role.add_permission!(:view_project_stages_and_gates)+ end++ it "allows filtering the projects by the life cycle gate" do+ load_and_open_filters manager++ projects_page.set_filter("lcsd_gate_#{gate.id}",+ "Lifecycle gate: #{gate.name}",+ "on",+ [Time.zone.today])++ projects_page.expect_projects_not_listed(development_project, public_project)+ projects_page.expect_projects_in_order(project)++ projects_page.remove_filter("lcsd_gate_#{gate.id}")++ projects_page.expect_projects_in_order(development_project, project, public_project)++ projects_page.set_filter("lcsd_gate_#{gate.id}",+ "Lifecycle gate: #{gate.name}",+ "today")++ projects_page.expect_projects_not_listed(development_project, public_project)+ projects_page.expect_projects_in_order(project)++ projects_page.remove_filter("lcsd_gate_#{gate.id}")++ projects_page.expect_projects_in_order(development_project, project, public_project)++ projects_page.set_filter("lcsd_gate_#{gate.id}",+ "Lifecycle gate: #{gate.name}",+ "between",+ [Time.zone.today - 5.days, Time.zone.today + 10.days])++ projects_page.expect_projects_not_listed(development_project, public_project)+ projects_page.expect_projects_in_order(project)++ projects_page.remove_filter("lcsd_gate_#{gate.id}")++ projects_page.expect_projects_in_order(development_project, project, public_project)++ projects_page.set_filter("lcsd_gate_#{gate.id}",+ "Lifecycle gate: #{gate.name}",+ "this week")++ projects_page.expect_projects_not_listed(development_project, public_project)+ projects_page.expect_projects_in_order(project)+ end+ end++ context "without the necessary permissions" do+ it "does not have the lifecycle (specific gate) filter" do+ load_and_open_filters manager++ projects_page.expect_filter_not_available("Lifecycle gate: #{gate.name}")+ end+ end+ end+ end+ endenddiff --git a/spec/models/queries/projects/filters/any_stage_or_gate_filter_spec.rb b/spec/models/queries/projects/filters/any_stage_or_gate_filter_spec.rbindex 295a2d3b58f..42862444923 100644--- a/spec/models/queries/projects/filters/any_stage_or_gate_filter_spec.rb+++ b/spec/models/queries/projects/filters/any_stage_or_gate_filter_spec.rb@@ -2,7 +2,7 @@# -- copyright# OpenProject is an open source project management software.-# Copyright (C) 2010-2024 the OpenProject GmbH+# Copyright (C) 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.@@ -35,15 +35,8 @@ RSpec.describe Queries::Projects::Filters::AnyStageOrGateFilter dolet(:class_key) { :any_stage_or_gate }let(:type) { :date }let(:model) { Project }- let(:attribute) { :created_at }let(:values) { ["3"] }-- describe "human_name" do- it "is 'Any stage or gate'" do- expect(instance.human_name)- .to eql I18n.t("project.filters.any_stage_or_gate")- end- end+ let(:human_name) { I18n.t("project.filters.any_stage_or_gate") }describe "default_operator" doit "is 'today'" dodiff --git a/spec/models/queries/projects/filters/life_cycle_gate_filter_spec.rb b/spec/models/queries/projects/filters/life_cycle_gate_filter_spec.rbnew file mode 100644index 00000000000..56fd9999e8f--- /dev/null+++ b/spec/models/queries/projects/filters/life_cycle_gate_filter_spec.rb@@ -0,0 +1,149 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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"++RSpec.describe Queries::Projects::Filters::LifeCycleGateFilter do+ let(:gate) { build_stubbed(:project_gate_definition) }+ # Defined here and in the .all to check that the filter only works on gates+ let(:stage) { build_stubbed(:project_stage_definition) }+ let(:query) { build_stubbed(:project_query) }++ let(:instance) do+ described_class.create!(name: accessor, operator: "=", context: query)+ end++ before do+ allow(Project::LifeCycleStepDefinition)+ .to receive(:all)+ .and_return([stage, gate])+ end++ describe ".create!" do+ context "for an existing gate" do+ it "returns a filter based on the gate" do+ expect(described_class.create!(name: "lcsd_gate_#{gate.id}", context: query))+ .to be_a described_class+ end+ end++ context "for a non existing gate" do+ it "raise an error" do+ expect { described_class.create!(name: "lcsd_gate_-1", context: query) }+ .to raise_error Queries::Filters::InvalidError+ end+ end+ end++ describe ".all_for" do+ it "returns filters for all life cycle steps" do+ expect(described_class.all_for)+ .to all(be_a(described_class))++ expect(described_class.all_for.map(&:human_name))+ .to contain_exactly(I18n.t("project.filters.life_cycle_gate", gate: gate.name))+ end+ end++ describe ".key" do+ it "is a regex for matching lifecycle steps" do+ expect(described_class.key)+ .to eql(/\Alcsd_gate_(\d+)\z/)+ end+ end++ describe "human_name" do+ let(:accessor) { "lcsd_gate_#{gate.id}" }++ it "is the name of the gate with a prefix" do+ expect(instance.human_name)+ .to eql I18n.t("project.filters.life_cycle_gate", gate: gate.name)+ end+ end++ describe "#available?" do+ let(:project) { build_stubbed(:project) }+ let(:accessor) { "lcsd_gate_#{gate.id}" }+ let(:user) { build_stubbed(:user) }++ current_user { user }++ before do+ mock_permissions_for(user) do |mock|+ mock.allow_in_project(*permissions, project:)+ end+ end++ context "for a user with the necessary permission and the feature flag on", with_flag: { stages_and_gates: true } do+ let(:permissions) { %i[view_project_stages_and_gates] }++ it "is true" do+ expect(instance)+ .to be_available+ end+ end++ context "for a user with the necessary permission and the feature flag off", with_flag: { stages_and_gates: false } do+ let(:permissions) { %i[view_project_stages_and_gates] }++ it "is false" do+ expect(instance)+ .not_to be_available+ end+ end++ context "for a user without the necessary permission", with_flag: { stages_and_gates: true } do+ let(:permissions) { %i[view_project] }++ it "is false" do+ expect(instance)+ .not_to be_available+ end+ end+ end++ describe "#type" do+ let(:accessor) { "lcsd_gate_#{gate.id}" }++ it "is :date" do+ expect(instance.type)+ .to be :date+ end+ end++ describe "#name" do+ let(:accessor) { "lcsd_gate_#{gate.id}" }++ it "is the accessor" do+ expect(instance.name)+ .to eql accessor.to_sym+ end+ end+enddiff --git a/spec/models/queries/projects/filters/life_cycle_stage_filter_spec.rb b/spec/models/queries/projects/filters/life_cycle_stage_filter_spec.rbnew file mode 100644index 00000000000..141261db623--- /dev/null+++ b/spec/models/queries/projects/filters/life_cycle_stage_filter_spec.rb@@ -0,0 +1,149 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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"++RSpec.describe Queries::Projects::Filters::LifeCycleStageFilter do+ let(:stage) { build_stubbed(:project_stage_definition) }+ # Defined here and in the .all to check that the filter only works on gates+ let(:gate) { build_stubbed(:project_gate_definition) }+ let(:query) { build_stubbed(:project_query) }++ let(:instance) do+ described_class.create!(name: accessor, operator: "=", context: query)+ end++ before do+ allow(Project::LifeCycleStepDefinition)+ .to receive(:all)+ .and_return([stage, gate])+ end++ describe ".create!" do+ context "for an existing stage" do+ it "returns a filter based on the stage" do+ expect(described_class.create!(name: "lcsd_stage_#{stage.id}", context: query))+ .to be_a described_class+ end+ end++ context "for a non existing stage" do+ it "raise an error" do+ expect { described_class.create!(name: "lcsd_stage_-1", context: query) }+ .to raise_error Queries::Filters::InvalidError+ end+ end+ end++ describe ".all_for" do+ it "returns filters for all life cycle steps" do+ expect(described_class.all_for)+ .to all(be_a(described_class))++ expect(described_class.all_for.map(&:human_name))+ .to contain_exactly(I18n.t("project.filters.life_cycle_stage", stage: stage.name))+ end+ end++ describe ".key" do+ it "is a regex for matching lifecycle steps" do+ expect(described_class.key)+ .to eql(/\Alcsd_stage_(\d+)\z/)+ end+ end++ describe "human_name" do+ let(:accessor) { "lcsd_stage_#{stage.id}" }++ it "is the name of the stage with a prefix" do+ expect(instance.human_name)+ .to eql I18n.t("project.filters.life_cycle_stage", stage: stage.name)+ end+ end++ describe "#available?" do+ let(:project) { build_stubbed(:project) }+ let(:accessor) { "lcsd_stage_#{stage.id}" }+ let(:user) { build_stubbed(:user) }++ current_user { user }++ before do+ mock_permissions_for(user) do |mock|+ mock.allow_in_project(*permissions, project:)+ end+ end++ context "for a user with the necessary permission and the feature flag on", with_flag: { stages_and_gates: true } do+ let(:permissions) { %i[view_project_stages_and_gates] }++ it "is true" do+ expect(instance)+ .to be_available+ end+ end++ context "for a user with the necessary permission and the feature flag off", with_flag: { stages_and_gates: false } do+ let(:permissions) { %i[view_project_stages_and_gates] }++ it "is false" do+ expect(instance)+ .not_to be_available+ end+ end++ context "for a user without the necessary permission", with_flag: { stages_and_gates: true } do+ let(:permissions) { %i[view_project] }++ it "is false" do+ expect(instance)+ .not_to be_available+ end+ end+ end++ describe "#type" do+ let(:accessor) { "lcsd_stage_#{stage.id}" }++ it "is :date" do+ expect(instance.type)+ .to be :date+ end+ end++ describe "#name" do+ let(:accessor) { "lcsd_stage_#{stage.id}" }++ it "is the accessor" do+ expect(instance.name)+ .to eql accessor.to_sym+ end+ end+enddiff --git a/spec/models/queries/projects/project_query_results_any_stage_or_gate_spec.rb b/spec/models/queries/projects/project_query_results_any_stage_or_gate_spec.rbindex 71948b8d8dc..f3b4eb3ea1b 100644--- a/spec/models/queries/projects/project_query_results_any_stage_or_gate_spec.rb+++ b/spec/models/queries/projects/project_query_results_any_stage_or_gate_spec.rb@@ -2,7 +2,7 @@# -- copyright# OpenProject is an open source project management software.-# Copyright (C) 2010-2024 the OpenProject GmbH+# Copyright (C) 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.@@ -336,7 +336,7 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" doendend- context "when being on the day of the gate but with the stage being disabled" do+ context "when being on the day of the gate but with the gate being disabled" dodisable_gateit "returns no project" dodiff --git a/spec/models/queries/projects/project_query_results_life_cycle_gate_spec.rb b/spec/models/queries/projects/project_query_results_life_cycle_gate_spec.rbnew file mode 100644index 00000000000..b79771a0de5--- /dev/null+++ b/spec/models/queries/projects/project_query_results_life_cycle_gate_spec.rb@@ -0,0 +1,411 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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"++RSpec.describe ProjectQuery, "results of a life cycle gate filter" do+ let(:instance) { described_class.new }++ shared_let(:view_role) { create(:project_role, permissions: %i[view_project_stages_and_gates]) }++ shared_let(:stage_start_date) { Date.parse("2025-02-07") }+ shared_let(:stage_end_date) { Date.parse("2025-02-17") }+ shared_let(:project_with_stage) { create(:project, name: "Project with stage") }+ shared_let(:stage) do+ create(:project_stage, project: project_with_stage, start_date: stage_start_date, end_date: stage_end_date)+ end++ shared_let(:gate_date) { Date.parse("2025-03-06") }+ shared_let(:project_with_gate) { create(:project, name: "Project with gate") }+ shared_let(:gate) { create(:project_gate, project: project_with_gate, date: gate_date) }++ shared_let(:project_without_step) { create(:project, name: "Project without step") }++ shared_let(:user) do+ create(:user, member_with_permissions: {+ project_with_stage => %i[view_project_stages_and_gates],+ project_with_gate => %i[view_project_stages_and_gates],+ project_without_step => %i[view_project_stages_and_gates]+ })+ end++ current_user { user }++ # rubocop:disable RSpec/ScatteredSetup+ def self.disable_gate+ before do+ Project::LifeCycleStep.where(type: Project::Gate.name).update_all(active: false)+ end+ end++ def self.remove_gate_dates+ before do+ gate.update_columns(end_date: nil, start_date: nil)+ end+ end++ def self.remove_permissions+ before do+ # We keep the permission within the project without steps so that the filter itself is available+ # but we check that the filter does not return values.+ RolePermission+ .where(role_id: Role.joins(:member_roles)+ .where(member_roles: { member_id: Member.where(project: [project_with_stage,+ project_with_gate]) }))+ .where(permission: :view_project_stages_and_gates)+ .destroy_all+ end+ end+ # rubocop:enable RSpec/ScatteredSetup++ context "with a =d (on) operator" do+ before do+ instance.where("lcsd_gate_#{gate.id}", "=d", values)+ end++ context "when filtering on the day of the gate" do+ let(:values) { [gate_date.to_s] }++ it "returns the project whose gate is on the date of the value" do+ expect(instance.results).to contain_exactly(project_with_gate)+ end+ end++ context "when filtering before the gate" do+ let(:values) { [(gate_date - 1.day).to_s] }++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when filtering after the gate" do+ let(:values) { [(gate_date + 1.day).to_s] }++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when the gate has no dates" do+ let(:values) { [gate_date.to_s] }++ remove_gate_dates++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when filtering on the day of the gate but with the gate being inactive" do+ let(:values) { [gate_date.to_s] }++ disable_gate++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when filtering on the day of the gate but without permissions" do+ let(:values) { [gate_date.to_s] }++ remove_permissions++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end+ end++ context "with a t (today) operator" do+ before do+ instance.where("lcsd_gate_#{gate.id}", "t", [])+ end++ context "when being on the day of the gate" do+ it "returns the project whose gate is on the date of the value" do+ Timecop.travel(gate_date.noon) do+ expect(instance.results).to contain_exactly(project_with_gate)+ end+ end+ end++ context "when being before the day of the gate" do+ it "returns no project" do+ Timecop.travel(gate_date.noon - 1.day) do+ expect(instance.results).to be_empty+ end+ end+ end++ context "when being after the day of the gate" do+ it "returns no project" do+ Timecop.travel(gate_date.noon + 1.day) do+ expect(instance.results).to be_empty+ end+ end+ end++ context "when the gate has no dates" do+ remove_gate_dates++ it "returns no project" do+ Timecop.travel(gate_date.noon) do+ expect(instance.results).to be_empty+ end+ end+ end++ context "when being on the day of the gate but with the gate being disabled" do+ disable_gate++ it "returns no project" do+ Timecop.travel(gate_date.noon) do+ expect(instance.results).to be_empty+ end+ end+ end++ context "when being on the day of the gate but without permissions" do+ remove_permissions++ it "returns no project" do+ Timecop.travel(gate_date.noon) do+ expect(instance.results).to be_empty+ end+ end+ end+ end++ context "with a w (this week) operator" do+ before do+ instance.where("lcsd_gate_#{gate.id}", "w", [])+ end++ context "when being a day before the gate" do+ it "returns the project whose gate is within the current week" do+ Timecop.travel(gate_date.noon - 1.day) do+ expect(instance.results).to contain_exactly(project_with_gate)+ end+ end+ end++ context "when being a day after the gate" do+ it "returns the project whose gate is within the current week" do+ Timecop.travel(gate_date.noon + 1.day) do+ expect(instance.results).to contain_exactly(project_with_gate)+ end+ end+ end++ context "when being in the week before the day of the gate" do+ it "returns no project" do+ Timecop.travel(gate_date.noon - 7.days) do+ expect(instance.results).to be_empty+ end+ end+ end++ context "when being in the week after the day of the gate" do+ it "returns no project" do+ Timecop.travel(gate_date.noon + 7.days) do+ expect(instance.results).to be_empty+ end+ end+ end++ context "when being a day before the gate but with the gate disabled" do+ disable_gate++ it "returns no project" do+ Timecop.travel(gate_date.noon - 1.day) do+ expect(instance.results).to be_empty+ end+ end+ end++ context "when being a day before the gate but without permissions" do+ remove_permissions++ it "returns no project" do+ Timecop.travel(gate_date.noon - 1.day) do+ expect(instance.results).to be_empty+ end+ end+ end++ context "when the gate has no dates" do+ remove_gate_dates++ it "returns no project" do+ Timecop.travel(gate_date.noon - 1.day) do+ expect(instance.results).to be_empty+ end+ end+ end++ context "when being on Monday, the gate is on Sunday and the week is configured to start on Sunday",+ with_settings: { start_of_week: 7 } do+ before do+ gate.update_column(:start_date, Date.parse("2025-03-02"))+ end++ it "returns the project whose gate is in the current week" do+ Timecop.travel(Date.parse("2025-03-03").noon) do+ expect(instance.results).to contain_exactly(project_with_gate)+ end+ end+ end++ context "when being on Monday, the gate is on Sunday and the week is configured to start on Monday",+ with_settings: { start_of_week: 1 } do+ before do+ gate.update_column(:start_date, Date.parse("2025-03-02"))+ end++ it "returns no project as the stage ended in the week before" do+ Timecop.travel(Date.parse("2025-03-03").noon) do+ expect(instance.results).to be_empty+ end+ end+ end++ context "when being on Sunday, the gate is on Monday and the week is configured to start on Monday",+ with_settings: { start_of_week: 1 } do+ before do+ gate.update_column(:start_date, Date.parse("2025-03-03"))+ end++ it "returns the project whose gate is in the current week" do+ Timecop.travel(Date.parse("2025-03-09").noon) do+ expect(instance.results).to contain_exactly(project_with_gate)+ end+ end+ end++ context "when being on Sunday, the gate is on Monday and the week is configured to start on Sunday",+ with_settings: { start_of_week: 7 } do+ before do+ gate.update_column(:start_date, Date.parse("2025-03-03"))+ end++ it "returns no project as the stage ended in the week before" do+ Timecop.travel(Date.parse("2025-03-09").noon) do+ expect(instance.results).to be_empty+ end+ end+ end+ end++ context "with a <>d (between) operator" do+ before do+ instance.where("lcsd_gate_#{gate.id}", "<>d", values)+ end++ context "when encompassing the gate completely" do+ let(:values) { [(gate_date - 1.day).to_s, (gate_date + 1.day).to_s] }++ it "returns the project with the gate" do+ expect(instance.results).to contain_exactly(project_with_gate)+ end+ end++ context "when encompassing the gate precisely" do+ let(:values) { [gate_date.to_s, gate_date.to_s] }++ it "returns the project with the gate" do+ expect(instance.results).to contain_exactly(project_with_gate)+ end+ end++ context "when only the lower value is provided and that one is before the gate's date" do+ let(:values) { [(gate_date - 1.day).to_s, ""] }++ it "returns the project with the gate" do+ expect(instance.results).to contain_exactly(project_with_gate)+ end+ end++ context "when only the upper value is provided and that one is after the gate's date" do+ let(:values) { ["", (gate_date + 1.day).to_s] }++ it "returns the project with the gate" do+ expect(instance.results).to contain_exactly(project_with_gate)+ end+ end++ context "when only the upper value is provided and that one is on the gate's date" do+ let(:values) { ["", gate_date.to_s] }++ it "returns the project with the gate" do+ expect(instance.results).to contain_exactly(project_with_gate)+ end+ end++ context "when only the upper value is provided and that one is before the gate's date" do+ let(:values) { ["", (gate_date - 1.day).to_s] }++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when the gate has no dates" do+ let(:values) { [(gate_date - 1.day).to_s, (gate_date + 1.day).to_s] }++ remove_gate_dates++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when encompassing the gate precisely but with the gate disabled" do+ let(:values) { [gate_date.to_s, gate_date.to_s] }++ disable_gate++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when encompassing the gate precisely but without permissions" do+ let(:values) { [gate_date.to_s, gate_date.to_s] }++ remove_permissions++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end+ end+enddiff --git a/spec/models/queries/projects/project_query_results_life_cycle_stage_spec.rb b/spec/models/queries/projects/project_query_results_life_cycle_stage_spec.rbnew file mode 100644index 00000000000..17a5e32ea3d--- /dev/null+++ b/spec/models/queries/projects/project_query_results_life_cycle_stage_spec.rb@@ -0,0 +1,481 @@+# frozen_string_literal: true++# -- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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"++RSpec.describe ProjectQuery, "results of a life cycle stage filter" do+ let(:instance) { described_class.new }++ shared_let(:view_role) { create(:project_role, permissions: %i[view_project_stages_and_gates]) }++ shared_let(:stage_start_date) { Date.parse("2025-02-07") }+ shared_let(:stage_end_date) { Date.parse("2025-02-17") }+ shared_let(:project_with_stage) { create(:project, name: "Project with stage") }+ shared_let(:stage) do+ create(:project_stage, project: project_with_stage, start_date: stage_start_date, end_date: stage_end_date)+ end++ shared_let(:gate_date) { Date.parse("2025-03-06") }+ shared_let(:project_with_gate) { create(:project, name: "Project with gate") }+ shared_let(:gate) { create(:project_gate, project: project_with_gate, date: gate_date) }++ shared_let(:project_without_step) { create(:project, name: "Project without step") }++ shared_let(:user) do+ create(:user, member_with_permissions: {+ project_with_stage => %i[view_project_stages_and_gates],+ project_with_gate => %i[view_project_stages_and_gates],+ project_without_step => %i[view_project_stages_and_gates]+ })+ end++ current_user { user }++ # rubocop:disable RSpec/ScatteredSetup+ def self.disable_stage+ before do+ Project::LifeCycleStep.where(type: Project::Stage.name).update_all(active: false)+ end+ end++ def self.remove_stage_dates+ before do+ stage.update_columns(end_date: nil, start_date: nil)+ end+ end++ def self.remove_permissions+ before do+ # We keep the permission within the project without steps so that the filter itself is available+ # but we check that the filter does not return values.+ RolePermission+ .where(role_id: Role.joins(:member_roles)+ .where(member_roles: { member_id: Member.where(project: [project_with_stage,+ project_with_gate]) }))+ .where(permission: :view_project_stages_and_gates)+ .destroy_all+ end+ end+ # rubocop:enable RSpec/ScatteredSetup++ context "with a =d (on) operator" do+ before do+ instance.where("lcsd_stage_#{stage.id}", "=d", values)+ end++ context "when filtering in the middle of the stage" do+ let(:values) { [(stage_start_date + ((stage_end_date - stage_start_date) / 2)).to_s] }++ it "returns the project whose stage is covering an interval including the date" do+ expect(instance.results).to contain_exactly(project_with_stage)+ end+ end++ context "when filtering on the first day of the stage" do+ let(:values) { [stage_start_date.to_s] }++ it "returns the project whose stage begins on that date" do+ expect(instance.results).to contain_exactly(project_with_stage)+ end+ end++ context "when filtering on the last day of the stage" do+ let(:values) { [stage_end_date.to_s] }++ it "returns the project whose stage ends on that date" do+ expect(instance.results).to contain_exactly(project_with_stage)+ end+ end++ context "when filtering before the stage" do+ let(:values) { [(stage_start_date - 1.day).to_s] }++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when filtering after the stage" do+ let(:values) { [(stage_end_date + 1.day).to_s] }++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when the stage has no dates" do+ let(:values) { [stage_end_date.to_s] }++ remove_stage_dates++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when filtering in the middle of the stage but with the stage being inactive" do+ let(:values) { [(stage_start_date + ((stage_end_date - stage_start_date) / 2)).to_s] }++ disable_stage++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when filtering in the middle of the stage but without permissions" do+ let(:values) { [(stage_start_date + ((stage_end_date - stage_start_date) / 2)).to_s] }++ remove_permissions++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end+ end++ context "with a t (today) operator" do+ before do+ instance.where("lcsd_stage_#{stage.id}", "t", [])+ end++ context "when being in the middle of the stage" do+ it "returns the project whose stage is currently running" do+ Timecop.travel((stage_start_date + (stage_end_date - stage_start_date)).noon) do+ expect(instance.results).to contain_exactly(project_with_stage)+ end+ end+ end++ context "when being on the first day of the stage" do+ it "returns the project whose stage begins on that date" do+ Timecop.travel(stage_start_date.noon) do+ expect(instance.results).to contain_exactly(project_with_stage)+ end+ end+ end++ context "when being on the last day of the stage" do+ it "returns the project whose stage begins on that date" do+ Timecop.travel(stage_end_date.noon) do+ expect(instance.results).to contain_exactly(project_with_stage)+ end+ end+ end++ context "when being before the stage" do+ it "returns no project" do+ Timecop.travel(stage_start_date.noon - 1.day) do+ expect(instance.results).to be_empty+ end+ end+ end++ context "when being after the stage" do+ it "returns no project" do+ Timecop.travel(stage_end_date.noon + 1.day) do+ expect(instance.results).to be_empty+ end+ end+ end++ context "when the stage has no dates" do+ remove_stage_dates++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when being in the middle of the stage but with the stage being disabled" do+ disable_stage++ it "returns no project" do+ Timecop.travel((stage_start_date + (stage_end_date - stage_start_date)).noon) do+ expect(instance.results).to be_empty+ end+ end+ end++ context "when being in the middle of the stage but without permissions" do+ remove_permissions++ it "returns no project" do+ Timecop.travel((stage_start_date + (stage_end_date - stage_start_date)).noon) do+ expect(instance.results).to be_empty+ end+ end+ end+ end++ context "with a w (this week) operator" do+ before do+ instance.where("lcsd_stage_#{stage.id}", "w", [])+ end++ context "when being in the middle of the stage" do+ it "returns the project whose stage is currently running" do+ Timecop.travel((stage_start_date + 3.days).noon) do+ expect(instance.results).to contain_exactly(project_with_stage)+ end+ end+ end++ context "when the current week overlaps the beginning of the stage" do+ it "returns the project whose stage begins within the week" do+ Timecop.travel((stage_start_date - 1.day).noon) do+ expect(instance.results).to contain_exactly(project_with_stage)+ end+ end+ end++ context "when the current week overlaps the end of the stage" do+ it "returns the project whose stage begins within the week" do+ Timecop.travel(stage_end_date.noon) do+ expect(instance.results).to contain_exactly(project_with_stage)+ end+ end+ end++ context "when being before the stage" do+ it "returns no project" do+ Timecop.travel(stage_start_date.noon - 7.days) do+ expect(instance.results).to be_empty+ end+ end+ end++ context "when being after the stage" do+ it "returns no project" do+ Timecop.travel(stage_end_date.noon + 7.days) do+ expect(instance.results).to be_empty+ end+ end+ end++ context "when the stage has no dates" do+ remove_stage_dates++ it "returns no project" do+ Timecop.travel(stage_end_date.noon + 7.days) do+ expect(instance.results).to be_empty+ end+ end+ end++ context "when being in the middle of the stage but with the stage disabled" do+ disable_stage++ it "returns no project" do+ Timecop.travel((stage_start_date + 3.days).noon) do+ expect(instance.results).to be_empty+ end+ end+ end++ context "when being in the middle of the stage but without permissions" do+ remove_permissions++ it "returns no project" do+ Timecop.travel((stage_start_date + 3.days).noon) do+ expect(instance.results).to be_empty+ end+ end+ end++ context "when being on Monday, the stage ends on Sunday and the week is configured to start on Sunday",+ with_settings: { start_of_week: 7 } do+ before do+ stage.update_column(:end_date, Date.parse("2025-02-16"))+ end++ it "returns the project whose stage has ended within the current week" do+ Timecop.travel(Date.parse("2025-02-17").noon) do+ expect(instance.results).to contain_exactly(project_with_stage)+ end+ end+ end++ context "when being on Monday, the stage ends on Sunday and the week is configured to start on Monday",+ with_settings: { start_of_week: 1 } do+ before do+ stage.update_column(:end_date, Date.parse("2025-02-16"))+ end++ it "returns no project as the stage ended in the week before" do+ Timecop.travel(Date.parse("2025-02-17").noon) do+ expect(instance.results).to be_empty+ end+ end+ end++ context "when being on Sunday, the stage ends on Monday and the week is configured to start on Monday",+ with_settings: { start_of_week: 1 } do+ before do+ stage.update_column(:end_date, Date.parse("2025-02-17"))+ end++ it "returns the project whose stage has ended within the current week" do+ Timecop.travel(Date.parse("2025-02-23").noon) do+ expect(instance.results).to contain_exactly(project_with_stage)+ end+ end+ end++ context "when being on Sunday, the stage ends on Monday and the week is configured to start on Sunday",+ with_settings: { start_of_week: 7 } do+ before do+ stage.update_column(:end_date, Date.parse("2025-02-17"))+ end++ it "returns no project as the stage ended in the week before" do+ Timecop.travel(Date.parse("2025-02-23").noon) do+ expect(instance.results).to be_empty+ end+ end+ end+ end++ context "with a <>d (between) operator" do+ before do+ instance.where("lcsd_stage_#{stage.id}", "<>d", values)+ end++ context "when encompassing the stage completely" do+ let(:values) { [(stage_start_date - 1.day).to_s, (stage_end_date + 1.day).to_s] }++ it "returns the project with the stage" do+ expect(instance.results).to contain_exactly(project_with_stage)+ end+ end++ context "when encompassing the stage precisely" do+ let(:values) { [stage_start_date.to_s, stage_end_date.to_s] }++ it "returns the project with the stage" do+ expect(instance.results).to contain_exactly(project_with_stage)+ end+ end++ context "when the values overlap the stage's start date but not the end date" do+ let(:values) { [(stage_start_date - 1.day).to_s, (stage_end_date - 1.day).to_s] }++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when the values overlap the stage's end date but not the start date" do+ let(:values) { [(stage_start_date + 1.day).to_s, stage_end_date.to_s] }++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when the values are between the start and the end date of the stage" do+ let(:values) { [(stage_start_date + 1.day).to_s, (stage_end_date - 1.day).to_s] }++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when only the lower value is provided and that one is before the stage's start date" do+ let(:values) { [(stage_start_date - 1.day).to_s, ""] }++ it "returns the project with the stage" do+ expect(instance.results).to contain_exactly(project_with_stage)+ end+ end++ context "when only the lower value is provided and that one is on the stage's start date" do+ let(:values) { [stage_start_date.to_s, ""] }++ it "returns the project with the stage" do+ expect(instance.results).to contain_exactly(project_with_stage)+ end+ end++ context "when only the lower value is provided and that one after the stage's start date" do+ let(:values) { [(stage_start_date + 1.day).to_s, ""] }++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when only the upper value is provided and that one is after the stage's end date" do+ let(:values) { ["", (stage_end_date + 1.day).to_s] }++ it "returns the project with the stage" do+ expect(instance.results).to contain_exactly(project_with_stage)+ end+ end++ context "when the stage has no dates" do+ let(:values) { [(stage_start_date - 1.day).to_s, (stage_end_date + 1.day).to_s] }++ remove_stage_dates++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when no value is provided" do+ let(:values) { ["", ""] }++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when encompassing the stage completely but with the stage disabled" do+ let(:values) { [(stage_start_date - 1.day).to_s, (stage_end_date + 1.day).to_s] }++ disable_stage++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end++ context "when encompassing the stage completely but without permissions" do+ let(:values) { [(stage_start_date - 1.day).to_s, (stage_end_date + 1.day).to_s] }++ remove_permissions++ it "returns no project" do+ expect(instance.results).to be_empty+ end+ end+ end+end