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

Add duration column to the work package table

Dombi Attilacc7c2fd3a5a0
diff --git a/app/models/queries/work_packages/filter/duration_filter.rb b/app/models/queries/work_packages/filter/duration_filter.rb
index 11e96a5c12f..694c3550da1 100644
--- a/app/models/queries/work_packages/filter/duration_filter.rb
+++ b/app/models/queries/work_packages/filter/duration_filter.rb
@@ -34,9 +34,19 @@ class Queries::WorkPackages::Filter::DurationFilter <
def where
if operator == Queries::Operators::None.to_sym.to_s
- super + " OR #{WorkPackage.table_name}.duration=0"
+ <<~SQL.squish
+ #{super}
+ OR #{WorkPackage.table_name}.duration = 0
+ OR #{Type.table_name}.is_milestone = TRUE
+ SQL
else
- super
+ <<~SQL.squish
+ #{super} AND #{Type.table_name}.is_milestone = FALSE
+ SQL
end
end
+
+ def joins
+ :type
+ end
end
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index 376c5a51719..8ad49eb96c3 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -271,7 +271,7 @@ class WorkPackage < ApplicationRecord
# Returns true if the work_package is overdue
def overdue?
- !due_date.nil? && (due_date < Date.today) && !closed?
+ !due_date.nil? && (due_date < Time.zone.today) && !closed?
end
def milestone?
@@ -287,9 +287,13 @@ class WorkPackage < ApplicationRecord
end
end
- def estimated_hours=(h)
- converted_hours = (h.is_a?(String) ? h.to_hours : h)
- write_attribute :estimated_hours, !!converted_hours ? converted_hours : h
+ def estimated_hours=(hours)
+ converted_hours = (hours.is_a?(String) ? hours.to_hours : hours)
+ write_attribute :estimated_hours, !!converted_hours ? converted_hours : hours
+ end
+
+ def duration_in_hours
+ duration ? duration * 24 : nil
end
# aliasing subject to name
@@ -460,7 +464,7 @@ class WorkPackage < ApplicationRecord
return if time_entry_blank?(attributes)
attributes.reverse_merge!(user:,
- spent_on: Date.today)
+ spent_on: Time.zone.today)
time_entries.build(attributes)
end
@@ -474,7 +478,7 @@ class WorkPackage < ApplicationRecord
key = 'activity_id'
id = attributes[key]
- default_id = if id && id.present?
+ default_id = if id&.present?
Enumeration.exists? id: id, is_default: true, type: 'TimeEntryActivity'
else
true
diff --git a/config/constants/settings/definitions.rb b/config/constants/settings/definitions.rb
index 769d6076f60..74ff6cdd6a5 100644
--- a/config/constants/settings/definitions.rb
+++ b/config/constants/settings/definitions.rb
@@ -979,6 +979,10 @@ Settings::Definition.define do
add :work_package_startdate_is_adddate,
default: false
+ add :work_packages_duration_field_active,
+ default: Rails.env.development?,
+ format: :boolean
+
add :youtube_channel,
default: 'https://www.youtube.com/c/OpenProjectCommunity',
writable: false
diff --git a/docs/api/apiv3/components/schemas/work_package_model.yml b/docs/api/apiv3/components/schemas/work_package_model.yml
index 8444f00a33f..3393766098c 100644
--- a/docs/api/apiv3/components/schemas/work_package_model.yml
+++ b/docs/api/apiv3/components/schemas/work_package_model.yml
@@ -55,6 +55,13 @@ properties:
by the work packages's descendants. If manual scheduleManually is active, the
two dates can deviate.
readOnly: true
+ duration:
+ type: string
+ format: duration
+ description: |-
+ **(NOT IMPLEMENTED)** The amount of time in hours the work package needs to be completed.
+ Not available for milestone type of work packages.
+ readOnly: true
estimatedTime:
type: string
format: duration
diff --git a/docs/api/apiv3/paths/work_packages.yml b/docs/api/apiv3/paths/work_packages.yml
index 0c5e310f3ef..3f74a3d8167 100644
--- a/docs/api/apiv3/paths/work_packages.yml
+++ b/docs/api/apiv3/paths/work_packages.yml
@@ -46,6 +46,7 @@ get:
- due_date
- duplicated
- duplicates
+ - duration
- estimated_hours
- file_link_origin_id
- follows
diff --git a/lib/api/v3/work_packages/schema/work_package_schema_representer.rb b/lib/api/v3/work_packages/schema/work_package_schema_representer.rb
index 1cda6e9c543..ff36ecf4216 100644
--- a/lib/api/v3/work_packages/schema/work_package_schema_representer.rb
+++ b/lib/api/v3/work_packages/schema/work_package_schema_representer.rb
@@ -117,6 +117,18 @@ module API
type: 'Formattable',
required: false
+ schema :duration,
+ type: 'Duration',
+ required: false,
+ writable: false,
+ show_if: ->(*) { !represented.milestone? && OpenProject::FeatureDecisions.work_packages_duration_field_active? }
+
+ # TODO: Remove this, once the duration feature is complete
+ # This enables hiding the duration field on the form_configuration
+ ::Type.add_constraint :duration, ->(*) {
+ OpenProject::FeatureDecisions.work_packages_duration_field_active?
+ }
+
schema :schedule_manually,
type: 'Boolean',
required: false,
diff --git a/lib/api/v3/work_packages/work_package_payload_representer.rb b/lib/api/v3/work_packages/work_package_payload_representer.rb
index df9edd8149b..8293edf578f 100644
--- a/lib/api/v3/work_packages/work_package_payload_representer.rb
+++ b/lib/api/v3/work_packages/work_package_payload_representer.rb
@@ -36,7 +36,13 @@ module API
cached_representer disabled: true
def writeable_attributes
- super + %w[date]
+ attributes = super
+ # TODO: Remove this, once the duration feature is complete
+ # We have to remove the duration field as it is not being accepted yet.
+ if OpenProject::FeatureDecisions.work_packages_duration_field_active?
+ attributes -= %w[duration]
+ end
+ attributes + %w[date]
end
def load_complete_model(model)
diff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rb
index e9f460fec40..e70c9c2ad51 100644
--- a/lib/api/v3/work_packages/work_package_representer.rb
+++ b/lib/api/v3/work_packages/work_package_representer.rb
@@ -391,6 +391,17 @@ module API
end,
render_nil: true
+ property :duration,
+ exec_context: :decorator,
+ skip_render: ->(represented:, **) {
+ represented.milestone? || !OpenProject::FeatureDecisions.work_packages_duration_field_active?
+ },
+ getter: ->(*) do
+ datetime_formatter.format_duration_from_hours(represented.duration_in_hours,
+ allow_nil: true)
+ end,
+ render_nil: true
+
property :spent_time,
exec_context: :decorator,
getter: ->(*) do
diff --git a/lib/open_project/feature_decisions.rb b/lib/open_project/feature_decisions.rb
index a3b49b1f482..1cf3082126b 100644
--- a/lib/open_project/feature_decisions.rb
+++ b/lib/open_project/feature_decisions.rb
@@ -31,5 +31,9 @@ module OpenProject
def self.storages_module_active?
Setting.feature_storages_module_active
end
+
+ def self.work_packages_duration_field_active?
+ Setting.work_packages_duration_field_active
+ end
end
end
diff --git a/spec/factories/type_factory.rb b/spec/factories/type_factory.rb
index 17dde98734f..5530e7e27d2 100644
--- a/spec/factories/type_factory.rb
+++ b/spec/factories/type_factory.rb
@@ -31,8 +31,8 @@ FactoryBot.define do
sequence(:position)
name { |a| "Type No. #{a.position}" }
description { nil }
- created_at { Time.now }
- updated_at { Time.now }
+ created_at { Time.zone.now }
+ updated_at { Time.zone.now }
factory :type_with_workflow, class: 'Type' do
callback(:after_build) do |t|
@@ -53,21 +53,25 @@ FactoryBot.define do
["query_#{query.id}".to_sym]]]
end
end
+
+ factory :type_milestone, class: 'Type' do
+ is_milestone { true }
+ end
end
factory :type_standard, class: '::Type' do
name { 'None' }
is_standard { true }
is_default { true }
- created_at { Time.now }
- updated_at { Time.now }
+ created_at { Time.zone.now }
+ updated_at { Time.zone.now }
end
factory :type_bug, class: '::Type' do
name { 'Bug' }
position { 1 }
- created_at { Time.now }
- updated_at { Time.now }
+ created_at { Time.zone.now }
+ updated_at { Time.zone.now }
# reuse existing type with the given name
# this prevents a validation error (name has to be unique)
diff --git a/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rb b/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rb
index 8101bcd48b4..1e6382424ce 100644
--- a/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rb
+++ b/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rb
@@ -97,7 +97,7 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
allow(schema).to receive(:writable?).and_call_original
end
- context 'generation' do
+ context 'for generation' do
subject(:generated) { representer.to_json }
shared_examples_for 'has a collection of allowed values' do
@@ -152,7 +152,7 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
let(:link) { 'baseSchema' }
end
- context 'embedded in a form' do
+ context 'when embedded in a form' do
let(:self_link) { nil }
let(:base_schema_link) { '/a/schema/link' }
@@ -243,7 +243,7 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
let(:writable) { true }
end
- context 'lockVersion disabled' do
+ context 'when lockVersion disabled' do
let(:representer) do
described_class.create(schema,
self_link: nil,
@@ -293,6 +293,50 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
end
end
+ describe 'duration' do
+ before do
+ # TODO: remove feature flag once the implementation is complete
+ allow(OpenProject::FeatureDecisions)
+ .to receive(:work_packages_duration_field_active?)
+ .and_return(true)
+ allow(schema)
+ .to receive(:milestone?)
+ .and_return(false)
+ end
+
+ it_behaves_like 'has basic schema properties' do
+ let(:path) { 'duration' }
+ let(:type) { 'Duration' }
+ let(:name) { I18n.t('activerecord.attributes.work_package.duration') }
+ let(:required) { false }
+ let(:writable) { false }
+ end
+
+ context 'when the work package is a milestone' do
+ before do
+ allow(schema)
+ .to receive(:milestone?)
+ .and_return(true)
+ end
+
+ it 'has no duration attribute' do
+ expect(subject).not_to have_json_path('duration')
+ end
+ end
+
+ context 'when the feature flag is off' do
+ before do
+ allow(OpenProject::FeatureDecisions)
+ .to receive(:work_packages_duration_field_active?)
+ .and_return(false)
+ end
+
+ it 'has no duration attribute' do
+ expect(subject).not_to have_json_path('duration')
+ end
+ end
+ end
+
describe 'scheduleManually' do
it_behaves_like 'has basic schema properties' do
let(:path) { 'scheduleManually' }
@@ -324,7 +368,7 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
let(:writable) { true }
end
- context 'not writable' do
+ context 'when not writable' do
before do
allow(schema)
.to receive(:writable?)
@@ -374,7 +418,7 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
let(:writable) { true }
end
- context 'not writable' do
+ context 'when not writable' do
before do
allow(schema)
.to receive(:writable?)
@@ -424,7 +468,7 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
let(:writable) { true }
end
- context 'not writable' do
+ context 'when not writable' do
before do
allow(schema).to receive(:writable?).with(:due_date).and_return false
end
@@ -519,7 +563,7 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
let(:writable) { true }
end
- context 'not writable' do
+ context 'when not writable' do
before do
allow(schema)
.to receive(:writable?)
@@ -592,7 +636,7 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
let(:writable) { true }
end
- context 'not writable' do
+ context 'when not writable' do
before do
allow(schema).to receive(:writable?).with(:percentage_done).and_return false
end
@@ -606,7 +650,7 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
end
end
- context 'is disabled' do
+ context 'as disabled' do
before do
allow(Setting).to receive(:work_package_done_ratio).and_return('disabled')
end
@@ -839,7 +883,7 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
let(:factory) { :priority }
end
- context 'not writable' do
+ context 'when not writable' do
before do
allow(schema).to receive(:writable?).with(:priority).and_return false
end
@@ -929,7 +973,7 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
end
describe 'budget' do
- context 'user allowed to view_budgets' do
+ context 'when user allowed to view_budgets' do
let(:permissions) { %i[edit_work_packages view_budgets] }
it_behaves_like 'has basic schema properties' do
@@ -948,7 +992,7 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
end
end
- context 'user not allowed to view_budgets' do
+ context 'when user not allowed to view_budgets' do
it 'has no schema for budget' do
expect(subject).not_to have_json_path('budget')
end
@@ -959,9 +1003,11 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
let(:available_custom_fields) { [build_stubbed(:int_wp_custom_field)] }
it 'uses a CustomFieldInjector' do
- expect(::API::V3::Utilities::CustomFieldInjector).to receive(:create_schema_representer)
+ allow(::API::V3::Utilities::CustomFieldInjector).to receive(:create_schema_representer)
.and_return(described_class)
representer.to_json
+
+ expect(::API::V3::Utilities::CustomFieldInjector).to have_received(:create_schema_representer)
end
end
end
@@ -972,10 +1018,12 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
let(:attribute_groups) { [] }
it 'is disabled' do
- expect(OpenProject::Cache)
- .not_to receive(:fetch)
+ allow(OpenProject::Cache).to receive(:fetch)
representer.to_json
+
+ expect(OpenProject::Cache)
+ .not_to have_received(:fetch)
end
end
@@ -1002,12 +1050,16 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
end
it 'is based on the representer\'s cache_key' do
- expect(OpenProject::Cache)
+ allow(OpenProject::Cache)
.to receive(:fetch)
.with(representer.json_cache_key)
.and_call_original
representer.to_json
+
+ expect(OpenProject::Cache)
+ .to have_received(:fetch)
+ .with(representer.json_cache_key)
end
it 'does not cache the attribute_groups' do
@@ -1038,7 +1090,7 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
change
end
- let(:setup) {}
+ let(:setup) { nil }
let(:original_cache_key) { joined_cache_key }
shared_examples_for 'changes' do
@@ -1051,7 +1103,7 @@ describe ::API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
end
end
- context 'for a different type' do
+ context 'for a different project' do
it_behaves_like 'changes' do
let(:change) { work_package.project = build_stubbed(:project) }
end
diff --git a/spec/lib/api/v3/work_packages/work_package_payload_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_payload_representer_spec.rb
index 20e5cb6105b..afacb57ddd0 100644
--- a/spec/lib/api/v3/work_packages/work_package_payload_representer_spec.rb
+++ b/spec/lib/api/v3/work_packages/work_package_payload_representer_spec.rb
@@ -33,8 +33,8 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
let(:work_package) do
build_stubbed(:work_package,
- start_date: Date.today.to_datetime,
- due_date: Date.today.to_datetime,
+ start_date: Time.zone.today.to_datetime,
+ due_date: Time.zone.today.to_datetime,
created_at: DateTime.now,
updated_at: DateTime.now,
budget:,
@@ -55,7 +55,7 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
let(:budget) { build_stubbed(:budget) }
let(:representer) do
- ::API::V3::WorkPackages::WorkPackagePayloadRepresenter
+ described_class
.create(work_package, current_user: user)
end
@@ -67,7 +67,7 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
.and_return(1)
end
- context 'generation' do
+ context 'for generation' do
subject(:generated) { representer.to_json }
describe 'work_package' do
@@ -76,7 +76,7 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
it_behaves_like 'API V3 formattable', 'description' do
let(:format) { 'markdown' }
let(:raw) { work_package.description }
- let(:html) { '<p class="op-uc-p">' + work_package.description + '</p>' }
+ let(:html) { "<p class=\"op-uc-p\">#{work_package.description}</p>" }
end
describe 'lock version' do
@@ -115,11 +115,11 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
.at_path('estimatedTime')
end
- context 'not set' do
+ context 'when not set' do
it { is_expected.to have_json_type(NilClass).at_path('estimatedTime') }
end
- context 'set' do
+ context 'when set' do
let(:work_package) { build(:work_package, estimated_hours: 0) }
it { is_expected.to have_json_type(String).at_path('estimatedTime') }
@@ -133,7 +133,7 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
.and_return(true)
end
- context 'percentage done enabled' do
+ context 'when percentage done enabled' do
it { is_expected.to have_json_path('percentageDone') }
it { is_expected.to have_json_type(Integer).at_path('percentageDone') }
@@ -143,7 +143,7 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
end
%w(disabled status).each do |setting|
- context "work package done ratio setting on #{setting}" do
+ context "when work package done ratio setting on #{setting}" do
before do
allow(Setting).to receive(:work_package_done_ratio).and_return(setting)
end
@@ -171,7 +171,7 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
let(:json_path) { 'startDate' }
end
- context 'no start date' do
+ context 'with no start date' do
let(:work_package) { build(:work_package, start_date: nil) }
it 'renders as null' do
@@ -179,7 +179,7 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
end
end
- context 'if the work_package is a milestone' do
+ context 'when the work_package is a milestone' do
before do
allow(work_package)
.to receive(:milestone?)
@@ -208,7 +208,7 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
let(:json_path) { 'dueDate' }
end
- context 'no finish date' do
+ context 'when no finish date' do
let(:work_package) { build(:work_package, due_date: nil) }
it 'renders as null' do
@@ -216,7 +216,7 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
end
end
- context 'if the work_package is a milestone' do
+ context 'when the work_package is a milestone' do
before do
allow(work_package)
.to receive(:milestone?)
@@ -245,7 +245,7 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
let(:json_path) { 'date' }
end
- context 'no finish date' do
+ context 'when no finish date' do
let(:work_package) do
build_stubbed(:work_package,
type: build_stubbed(:type),
@@ -257,7 +257,7 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
end
end
- context 'if the work_package is no milestone' do
+ context 'when the work_package is no milestone' do
before do
allow(work_package)
.to receive(:milestone?)
@@ -269,6 +269,32 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
end
end
end
+
+ describe 'duration' do
+ before do
+ allow(OpenProject::FeatureDecisions)
+ .to receive(:work_packages_duration_field_active?)
+ .and_return(true)
+ end
+
+ it 'has no duration attribute' do
+ # TODO: Remove this, once the duration feature is complete
+ expect(subject).not_to have_json_path('duration')
+ end
+
+ context 'when duration feature flag disabled' do
+ before do
+ allow(OpenProject::FeatureDecisions)
+ .to receive(:work_packages_duration_field_active?)
+ .and_return(false)
+ end
+
+ it 'has no duration attribute' do
+ # TODO: Remove this, once the duration feature is complete
+ expect(subject).not_to have_json_path('duration')
+ end
+ end
+ end
end
describe '_links' do
@@ -450,9 +476,10 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
let(:available_custom_fields) { [build_stubbed(:int_wp_custom_field)] }
it 'uses a CustomFieldInjector' do
- expect(::API::V3::Utilities::CustomFieldInjector).to receive(:create_value_representer)
+ allow(::API::V3::Utilities::CustomFieldInjector).to receive(:create_value_representer)
.and_call_original
representer.to_json
+ expect(::API::V3::Utilities::CustomFieldInjector).to have_received(:create_value_representer)
end
end
@@ -460,10 +487,13 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
it 'does not cache' do
representer.to_json
- expect(Rails.cache)
- .not_to receive(:fetch)
+ allow(Rails.cache)
+ .to receive(:fetch)
representer.to_json
+
+ expect(Rails.cache)
+ .not_to have_received(:fetch)
end
end
end
@@ -476,21 +506,19 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
copy[:_links] = links
copy.to_json
end
- let(:work_package) do
- OpenStruct.new available_custom_fields:
- end
+ let(:work_package) { WorkPackage.new }
subject { representer.from_json(json) }
shared_examples_for 'settable ISO 8601 date only' do
let(:attributes) do
{
- property => dateString
+ property => date_string
}
end
context 'with an ISO formatted date' do
- let(:dateString) { '2015-01-31' }
+ let(:date_string) { '2015-01-31' }
it 'sets the date' do
expect(subject.send(method)).to eql(Date.new(2015, 1, 31))
@@ -498,7 +526,7 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
end
context 'with null' do
- let(:dateString) { nil }
+ let(:date_string) { nil }
it 'sets the date to nil' do
expect(subject.send(method)).to be_nil
@@ -506,7 +534,7 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
end
context 'with a non ISO formatted date' do
- let(:dateString) { '31.01.2015' }
+ let(:date_string) { '31.01.2015' }
it 'raises an error' do
expect { subject }.to raise_error(API::Errors::PropertyFormatError)
@@ -514,7 +542,7 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
end
context 'with an ISO formatted date and time' do
- let(:dateString) { '2015-01-31T13:37:00Z' }
+ let(:date_string) { '2015-01-31T13:37:00Z' }
it 'raises an error' do
expect { subject }.to raise_error(API::Errors::PropertyFormatError)
@@ -565,7 +593,7 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
.and_return(true)
# Setting values to ensure they are later set to nil in one of the test cases
- work_package.start_date = work_package.due_date = Date.today
+ work_package.start_date = work_package.due_date = Time.zone.today
end
it_behaves_like 'settable ISO 8601 date only' do
@@ -573,7 +601,7 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
let(:method) { :due_date }
context 'with an ISO formatted date' do
- let(:dateString) { '2015-01-31' }
+ let(:date_string) { '2015-01-31' }
it 'sets the start and the due_date' do
expect(subject.start_date).to eql(Date.new(2015, 1, 31))
@@ -585,7 +613,7 @@ describe ::API::V3::WorkPackages::WorkPackagePayloadRepresenter do
shared_examples_for 'linked resource' do
let(:path) { api_v3_paths.send(attribute_name, id) }
- let(:association_name) { attribute_name + '_id' }
+ let(:association_name) { "#{attribute_name}_id" }
let(:id) { work_package.send(association_name).to_i + 1 }
let(:links) do
{ attribute_name => href }
diff --git a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
index b00562d6c59..df0ebb1a0d6 100644
--- a/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
+++ b/spec/lib/api/v3/work_packages/work_package_representer_spec.rb
@@ -52,11 +52,13 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
let(:derived_start_date) { Time.zone.today - 4.days }
let(:derived_due_date) { Time.zone.today - 5.days }
let(:budget) { build_stubbed(:budget, project:) }
+ let(:duration) { nil }
let(:work_package) do
build_stubbed(:work_package,
schedule_manually:,
start_date:,
due_date:,
+ duration:,
done_ratio: 50,
parent:,
type:,
@@ -295,6 +297,49 @@ describe ::API::V3::WorkPackages::WorkPackageRepresenter do
end
end
+ describe 'duration' do
+ let(:duration) { 6 }
+
+ before do
+ # TODO: remove feature flag once the implementation is complete
+ allow(OpenProject::FeatureDecisions)
+ .to receive(:work_packages_duration_field_active?)
+ .and_return(true)
+ end
+
+ it { is_expected.to be_json_eql('P6D'.to_json).at_path('duration') }
+
+ context 'no duration' do
+ let(:duration) { nil }
+
+ it 'renders as null' do
+ is_expected.to be_json_eql(nil.to_json).at_path('duration')
+ end
+ end
+
+ context 'when the work_package is a milestone' do
+ let(:type_milestone) { true }
+
+ it 'has no duration' do
+ is_expected.to_not have_json_path('duration')
+ end
+ end
+
+ context 'when the feature flag is off' do
+ before do
+ # TODO: remove feature flag once the implementation is complete
+ allow(OpenProject::FeatureDecisions)
+ .to receive(:work_packages_duration_field_active?)
+ .and_return(false)
+ end
+ let(:duration) { 6 }
+
+ it 'has no duration' do
+ is_expected.to_not have_json_path('duration')
+ end
+ end
+ end
+
describe 'createdAt' do
it_behaves_like 'has UTC ISO 8601 date and time' do
let(:date) { work_package.created_at }
diff --git a/spec/models/queries/work_packages/filter/duration_filter_spec.rb b/spec/models/queries/work_packages/filter/duration_filter_spec.rb
index c996117d44d..575da1b1bb6 100644
--- a/spec/models/queries/work_packages/filter/duration_filter_spec.rb
+++ b/spec/models/queries/work_packages/filter/duration_filter_spec.rb
@@ -51,16 +51,24 @@ describe Queries::WorkPackages::Filter::DurationFilter, type: :model do
let!(:work_package_zero_duration) { create(:work_package, duration: 0) }
let!(:work_package_no_duration) { create(:work_package, duration: nil) }
let!(:work_package_with_duration) { create(:work_package, duration: 1) }
+ let!(:work_package_with_milestone) { create(:work_package, duration: 1, type: create(:type_milestone)) }
+ let(:values) { [1] }
+
+ subject { WorkPackage.joins(instance.joins).where(instance.where) }
context 'with the operator being "none"' do
before do
instance.operator = Queries::Operators::None.to_sym.to_s
end
- it 'finds zero and none values' do
- expect(WorkPackage.where(instance.where)).to match_array [work_package_zero_duration, work_package_no_duration]
+ it 'finds zero and none values also including milestones' do
+ expect(subject).to match_array [work_package_zero_duration, work_package_no_duration, work_package_with_milestone]
end
end
+
+ it 'does not returns milestone work packages' do
+ expect(subject).to match_array [work_package_with_duration]
+ end
end
end
end
diff --git a/spec/models/type_spec.rb b/spec/models/type_spec.rb
index de27e715095..45fb9fca9da 100644
--- a/spec/models/type_spec.rb
+++ b/spec/models/type_spec.rb
@@ -47,7 +47,7 @@ describe ::Type, type: :model do
end
describe '.statuses' do
- let(:subject) { type.statuses }
+ subject { type.statuses }
context 'when new' do
let(:type) { build(:type) }
@@ -85,7 +85,8 @@ describe ::Type, type: :model do
context 'with default status' do
let!(:default_status) { create(:default_status) }
- let(:subject) { type.statuses(include_default: true) }
+
+ subject { type.statuses(include_default: true) }
it 'returns the workflow and the default status' do
expect(subject.pluck(:id)).to contain_exactly(default_status.id, statuses[0].id, statuses[1].id)
@@ -108,4 +109,30 @@ describe ::Type, type: :model do
.with(type2, nil, type, nil)
end
end
+
+ describe '#work_package_attributes' do
+ subject { type.work_package_attributes }
+
+ before do
+ allow(OpenProject::FeatureDecisions)
+ .to receive(:work_packages_duration_field_active?)
+ .and_return(true)
+ end
+
+ it 'returns the duration field' do
+ expect(subject).to have_key("duration")
+ end
+
+ context 'when the feature flag is off' do
+ before do
+ allow(OpenProject::FeatureDecisions)
+ .to receive(:work_packages_duration_field_active?)
+ .and_return(false)
+ end
+
+ it 'does not return the duration field' do
+ expect(subject).not_to have_key("duration")
+ end
+ end
+ end
end