Skip to content

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

ensure null values are not returned

ulfertsbe400da0e386
diff --git a/app/models/queries/projects/filters/any_stage_or_gate_filter.rb b/app/models/queries/projects/filters/any_stage_or_gate_filter.rb
index b9359298a58..c60869dc209 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
@@ -93,41 +93,27 @@ class Queries::Projects::Filters::AnyStageOrGateFilter < Queries::Projects::Filt
end
def stage_where_on(start_date, end_date = start_date)
- 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: Project::Stage.name)
- .active
+ 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)
- 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: Project::Stage.name)
- .active
+ 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.
- 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: Project::Gate.name)
- .active
+ 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
- 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: Project::Stage.name)
- .active
+ 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,
@@ -139,6 +125,14 @@ class Queries::Projects::Filters::AnyStageOrGateFilter < Queries::Projects::Filt
)
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
diff --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.rb
index 6926295f483..71948b8d8dc 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
@@ -83,6 +83,18 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
end
end
+ def self.remove_stage_dates
+ before do
+ stage.update_columns(end_date: nil, start_date: nil)
+ 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
@@ -142,6 +154,16 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
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 on the day of the gate" do
let(:values) { [gate_date.to_s] }
@@ -166,6 +188,16 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
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 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] }
@@ -252,6 +284,14 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
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 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
@@ -276,6 +316,16 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
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 in the middle of the stage but with the stage being disabled" do
disable_stage
@@ -362,10 +412,17 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
end
end
- context "when being a day before the gate" do
- # Would otherwise interfere with the spec
- remove_stage
+ 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 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)
@@ -374,9 +431,6 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
end
context "when being a day after the gate" do
- # Would otherwise interfere with the spec
- remove_stage
-
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)
@@ -385,9 +439,6 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
end
context "when being in the week before the day of the gate" do
- # Would otherwise interfere with the spec
- remove_stage
-
it "returns no project" do
Timecop.travel(gate_date.noon - 7.days) do
expect(instance.results).to be_empty
@@ -414,8 +465,6 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
end
context "when being a day before the gate but with the gate disabled" do
- # Would otherwise interfere with the spec
- remove_stage
disable_gate
it "returns no project" do
@@ -436,8 +485,6 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
end
context "when being a day before the gate but without permissions" do
- # Would otherwise interfere with the spec
- remove_stage
remove_permissions
it "returns no project" do
@@ -447,6 +494,16 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
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 stage ends on Sunday and the week is configured to start on Sunday",
with_settings: { start_of_week: 7 } do
before do
@@ -638,6 +695,24 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
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 gate completely" do
let(:values) { [(gate_date - 1.day).to_s, (gate_date + 1.day).to_s] }
@@ -695,6 +770,16 @@ RSpec.describe ProjectQuery, "results of 'Any stage or gate' filter" do
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 stage completely but with the stage disabled" do
let(:values) { [(stage_start_date - 1.day).to_s, (stage_end_date + 1.day).to_s] }