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

setting project phase on work package

ulfertseec1525a9880
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rb
index 48f9018d609..c36fbd96abb 100644
--- a/app/contracts/work_packages/base_contract.rb
+++ b/app/contracts/work_packages/base_contract.rb
@@ -83,7 +83,13 @@ module WorkPackages
permission: :manage_subtasks
attribute :project_phase_definition_id,
- permission: :edit_project_phases
+ permission: :view_project_phases,
+ writable: ->(*) {
+ OpenProject::FeatureDecisions.stages_and_gates_active?
+ } do
+ validate_phase_active_in_project
+ end
+ attribute_alias :project_phase_definition_id, :project_phase_id
attribute :assigned_to_id do
next unless model.project
@@ -200,6 +206,18 @@ module WorkPackages
IssuePriority.active
end
+ def assignable_project_phases
+ if model.project
+ model
+ .project
+ .phases
+ .active
+ .order_by_position
+ else
+ Project::Phase.none
+ end
+ end
+
def assignable_versions(only_open: true)
model.try(:assignable_versions, only_open:) if model.project
end
@@ -527,6 +545,14 @@ module WorkPackages
end
end
+ def validate_phase_active_in_project
+ if model.project.present? &&
+ model.project_phase_definition_id.present? &&
+ !assignable_project_phases.exists?(definition_id: model.project_phase_definition_id)
+ errors.add :project_phase_id, :inclusion
+ end
+ end
+
def dates_derivation_impossible?
model.errors[:duration].any?
end
diff --git a/app/models/principals/scopes/possible_assignee.rb b/app/models/principals/scopes/possible_assignee.rb
index dfb5f0a382b..a9ebcaf596c 100644
--- a/app/models/principals/scopes/possible_assignee.rb
+++ b/app/models/principals/scopes/possible_assignee.rb
@@ -59,9 +59,9 @@ module Principals::Scopes
private
def resource_class(work_package_or_project)
- if work_package_or_project.all? { _1.class <= WorkPackage }
+ if work_package_or_project.all? { it.class <= WorkPackage }
WorkPackage
- elsif work_package_or_project.all? { _1.instance_of?(Project) }
+ elsif work_package_or_project.all? { it.instance_of?(Project) }
Project
end
end
diff --git a/app/models/project/phase.rb b/app/models/project/phase.rb
index c2773d9ab02..f42b5bc97a0 100644
--- a/app/models/project/phase.rb
+++ b/app/models/project/phase.rb
@@ -27,6 +27,8 @@
#++
class Project::Phase < ApplicationRecord
+ include ::Scopes::Scoped
+
belongs_to :project, optional: false, inverse_of: :available_phases
belongs_to :definition,
optional: false,
@@ -47,6 +49,7 @@ class Project::Phase < ApplicationRecord
attr_readonly :definition_id
scope :active, -> { where(active: true) }
+ scopes :order_by_position
class << self
def visible(user = User.current)
diff --git a/app/models/project/phases/scopes/order_by_position.rb b/app/models/project/phases/scopes/order_by_position.rb
new file mode 100644
index 00000000000..c6e2ad820c1
--- /dev/null
+++ b/app/models/project/phases/scopes/order_by_position.rb
@@ -0,0 +1,42 @@
+# 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 Project::Phases::Scopes
+ module OrderByPosition
+ extend ActiveSupport::Concern
+
+ class_methods do
+ def order_by_position
+ includes(:definition)
+ .order("#{Project::PhaseDefinition.table_name}.position ASC")
+ end
+ end
+ end
+end
diff --git a/frontend/src/app/shared/components/fields/edit/edit-field.initializer.ts b/frontend/src/app/shared/components/fields/edit/edit-field.initializer.ts
index ee82ecebce5..29b21b3c0f1 100644
--- a/frontend/src/app/shared/components/fields/edit/edit-field.initializer.ts
+++ b/frontend/src/app/shared/components/fields/edit/edit-field.initializer.ts
@@ -93,6 +93,7 @@ export function initializeCoreEditFields(editFieldService:EditFieldService, sele
.addFieldType(UserEditFieldComponent, 'user', ['User'])
.addFieldType(SelectEditFieldComponent, 'select', [
'Priority',
+ 'ProjectPhase',
'Status',
'Type',
'Version',
diff --git a/lib/api/v3/work_packages/schema/specific_work_package_schema.rb b/lib/api/v3/work_packages/schema/specific_work_package_schema.rb
index f465a1723af..0a00c6b9bc4 100644
--- a/lib/api/v3/work_packages/schema/specific_work_package_schema.rb
+++ b/lib/api/v3/work_packages/schema/specific_work_package_schema.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -54,6 +56,7 @@ module API
:assignable_priorities,
:assignable_versions,
:assignable_budgets,
+ :assignable_project_phases,
to: :contract
def no_caching?
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 35143a78742..bafa5de2398 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
@@ -226,13 +226,20 @@ module API
end
}
- # TODO: Turn into schema_with_allowed_link
- schema :project_phase,
- type: "ProjectPhase",
- location: :link,
- required: false,
- show_if: ->(*) { current_user.allowed_in_project?(:view_project_phases, represented.project) },
- writable: -> { represented.writable?(:project_phase_definition_id) }
+ schema_with_allowed_collection :project_phase,
+ value_representer: ProjectPhases::ProjectPhaseRepresenter,
+ link_factory: ->(phase) {
+ {
+ href: api_v3_paths.project_phase(phase.id),
+ title: phase.name
+ }
+ },
+ required: false,
+ show_if: ->(*) {
+ current_user.allowed_in_project?(:view_project_phases, represented.project) &&
+ OpenProject::FeatureDecisions.stages_and_gates_active?
+ },
+ writable: -> { represented.writable?(:project_phase_definition_id) }
schema_with_allowed_link :parent,
type: "WorkPackage",
diff --git a/lib/api/v3/work_packages/work_package_representer.rb b/lib/api/v3/work_packages/work_package_representer.rb
index afd2deeceec..300336aa123 100644
--- a/lib/api/v3/work_packages/work_package_representer.rb
+++ b/lib/api/v3/work_packages/work_package_representer.rb
@@ -509,8 +509,19 @@ module API
)
end
end,
- # TODO: write the setter
- setter: ->(*) {}
+ setter: ->(fragment:, **) do
+ link = ::API::Decorators::LinkObject.new(represented,
+ path: :project_phases,
+ property_name: :project_phase,
+ setter: :project_phase_id=)
+
+ link.from_hash(fragment)
+
+ represented.project_phase_definition_id = Project::Phase
+ .where(id: represented.project_phase_id)
+ .pick(:definition_id)
+ .to_s
+ end
associated_resource :status
@@ -656,7 +667,8 @@ module API
end
def view_project_phase_allowed?
- @view_project_phase_allowed ||= current_user.allowed_in_project?(:view_project_phases, represented.project)
+ @view_project_phase_allowed ||= current_user.allowed_in_project?(:view_project_phases, represented.project) &&
+ OpenProject::FeatureDecisions.stages_and_gates_active?
end
def export_work_packages_allowed?
diff --git a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb
index 20393e25323..2c1aa76efbe 100644
--- a/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb
+++ b/lib_static/plugins/acts_as_customizable/lib/acts_as_customizable.rb
@@ -278,6 +278,7 @@ module Redmine
# Skip when the new value is the default value
next cfv_changes if value_was.nil? && cfv.default?
+
cfv_changes.merge("custom_field_#{cfv.custom_field_id}" => [value_was, cfv.value])
end
end
diff --git a/spec/contracts/work_packages/create_contract_spec.rb b/spec/contracts/work_packages/create_contract_spec.rb
index 83074189d98..2582146550b 100644
--- a/spec/contracts/work_packages/create_contract_spec.rb
+++ b/spec/contracts/work_packages/create_contract_spec.rb
@@ -30,114 +30,65 @@ require "spec_helper"
require "contracts/work_packages/shared_base_contract"
RSpec.describe WorkPackages::CreateContract do
+ include_context "work package contract"
+
let(:work_package) do
- WorkPackage.new(project: work_package_project) do |wp|
+ WorkPackage.new(project: persisted_project,
+ subject: "Some subject",
+ type: persisted_type,
+ priority: persisted_priority,
+ status: persisted_status) do |wp|
wp.extend(OpenProject::ChangedBySystem)
+
+ wp.change_by_system do
+ wp.author = user
+ end
end
end
- let(:validated_contract) do
- contract = subject
- contract.validate
- contract
- end
- let(:work_package_project) { project }
- let(:project) { build_stubbed(:project) }
let(:other_project) { build_stubbed(:project) }
- let(:user) { build_stubbed(:user) }
+ let(:user) { persisted_user }
+ let(:permissions) { %i[add_work_packages assign_versions] }
subject(:contract) { described_class.new(work_package, user) }
- it_behaves_like "work package contract"
-
- describe "authorization" do
+ describe "validations" do
context "when user allowed in project and project specified" do
- before do
- mock_permissions_for(user) do |mock|
- mock.allow_in_project :add_work_packages, project:
- end
-
- work_package.project = project
- end
-
- it "has no authorization error" do
- expect(validated_contract.errors[:base]).to be_empty
- end
+ it_behaves_like "contract is valid"
end
context "when user not allowed in project and project specified" do
- before do
- mock_permissions_for(user) do |mock|
- mock.allow_in_project :add_work_packages, project: other_project
- end
+ let(:permissions) { [] }
- work_package.project = project
- end
-
- it "is not authorized" do
- expect(validated_contract.errors.symbols_for(:base))
- .to contain_exactly(:error_unauthorized)
- end
+ it_behaves_like "contract is invalid", base: :error_unauthorized
end
- context "when user allowed in a project and no project specified" do
+ context "when user not allowed in any project and no project specified" do
before do
- mock_permissions_for(user) do |mock|
- mock.allow_in_project :add_work_packages, project:
- end
+ work_package.project = nil
end
- it "has no authorization error" do
- expect(validated_contract.errors[:base]).to be_empty
- end
+ # But no error_unauthorized
+ it_behaves_like "contract is invalid", project: :blank
end
context "when user not allowed in any projects and no project specified" do
- before do
- mock_permissions_for(user, &:forbid_everything)
- end
+ let(:permissions) { [] }
- it "is not authorized" do
- expect(validated_contract.errors.symbols_for(:base))
- .to contain_exactly(:error_unauthorized)
- end
- end
-
- context "when user not allowed in any projects and project specified" do
before do
- mock_permissions_for(user, &:forbid_everything)
-
- work_package.project = project
+ work_package.project = nil
end
- it "is not authorized" do
- expect(validated_contract.errors.symbols_for(:base))
- .to contain_exactly(:error_unauthorized)
- end
+ it_behaves_like "contract is invalid", base: :error_unauthorized
end
end
- describe "remaining hours" do
- before do
- mock_permissions_for(user) do |mock|
- mock.allow_in_project :add_work_packages, project:
- end
- work_package.project = project
- end
-
- context "when not changed" do
- it("is valid") { expect(validated_contract.errors[:remaining_hours]).to be_empty }
- end
-
- context "when changed" do
- before do
- work_package.remaining_hours = 10
- end
-
- it("is valid") { expect(validated_contract.errors[:remaining_hours]).to be_empty }
+ describe "writing read-only attributes" do
+ let(:validated_contract) do
+ contract = subject
+ contract.validate
+ contract
end
- end
- describe "writing read-only attributes" do
shared_examples "can write" do |attribute, value|
it "can write #{attribute}", :aggregate_failures do
expect(contract.writable_attributes).to include(attribute.to_s)
@@ -185,4 +136,44 @@ RSpec.describe WorkPackages::CreateContract do
it_behaves_like "can not write", :author_id, 1234
end
end
+
+ describe "#assignable_assignees" do
+ context "with a project set" do
+ it "returns the users assignable" do
+ expect(subject.assignable_assignees)
+ .to contain_exactly(persisted_possible_assignee)
+ end
+ end
+
+ context "without a project set" do
+ before do
+ work_package.project = nil
+ end
+
+ it "returns no users" do
+ expect(subject.assignable_assignees)
+ .to be_empty
+ end
+ end
+ end
+
+ describe "#assignable_responsibles" do
+ context "with a project set" do
+ it "returns the users assignable" do
+ expect(subject.assignable_responsibles)
+ .to contain_exactly(persisted_possible_assignee)
+ end
+ end
+
+ context "without a project set" do
+ before do
+ work_package.project = nil
+ end
+
+ it "returns no users" do
+ expect(subject.assignable_responsibles)
+ .to be_empty
+ end
+ end
+ end
end
diff --git a/spec/contracts/work_packages/shared_base_contract.rb b/spec/contracts/work_packages/shared_base_contract.rb
index 7e02af9c51c..00dd481950a 100644
--- a/spec/contracts/work_packages/shared_base_contract.rb
+++ b/spec/contracts/work_packages/shared_base_contract.rb
@@ -26,8 +26,36 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-RSpec.shared_examples_for "work package contract" do
- let(:user) { build_stubbed(:user) }
+require "contracts/shared/model_contract_shared_context"
+
+RSpec.shared_examples "work package contract" do
+ create_shared_association_defaults_for_work_package_factory
+
+ include_context "ModelContract shared context"
+ shared_let(:persisted_type) { create(:type) }
+ shared_let(:persisted_project) { create(:project, types: [persisted_type]) }
+ shared_let(:persisted_project_version) { create(:version, project: persisted_project) }
+ shared_let(:persisted_other_project_version) { create(:version) }
+ shared_let(:persisted_status) { create(:status) }
+ shared_let(:persisted_priority) { create(:priority) }
+ shared_let(:other_persisted_project) { create(:project, types: [persisted_type]) }
+ shared_let(:persisted_possible_assignee) do
+ create(:user, member_with_permissions: { persisted_project => %i[view_work_packages work_package_assigned] })
+ end
+ shared_let(:persisted_non_member) { create(:user) }
+ shared_let(:persisted_user_role) do
+ create(:project_role, permissions: %i[view_work_packages])
+ end
+ shared_let(:persisted_project_phase_definition) { create(:project_phase_definition) }
+ shared_let(:persisted_active_project_phase) do
+ create(:project_phase, :active, project: persisted_project, definition: persisted_project_phase_definition)
+ end
+ shared_let(:persisted_inactive_project_phase) { create(:project_phase, :inactive, project: persisted_project) }
+
+ shared_let(:persisted_user) do
+ create(:user, member_with_roles: { persisted_project => persisted_user_role })
+ end
+
let(:other_user) { build_stubbed(:user) }
subject(:contract) { described_class.new(work_package, user) }
@@ -38,99 +66,195 @@ RSpec.shared_examples_for "work package contract" do
contract
end
- let(:possible_assignees) { [] }
- let!(:assignable_assignees_scope) do
- scope = double "assignable assignees scope"
-
- if work_package.persisted?
- allow(Principal)
- .to receive(:possible_assignee)
- .with(work_package)
- .and_return scope
- else
- allow(Principal)
- .to receive(:possible_assignee)
- .with(work_package.project)
- .and_return scope
+ before do
+ if user.members.any?
+ persisted_user_role.permissions = permissions
+ persisted_user_role.save
end
+ end
- allow(scope)
- .to receive(:exists?) do |hash|
- possible_assignees.map(&:id).include?(hash[:id])
+ describe "validations" do
+ context "when all attributes are valid" do
+ it_behaves_like "contract is valid"
end
- scope
- end
+ describe "assigned_to_id" do
+ context "if the assigned user is a possible assignee" do
+ before do
+ work_package.assigned_to = persisted_possible_assignee
+ end
- shared_examples_for "has no error on" do |property|
- it property do
- expect(validated_contract.errors[property]).to be_empty
- end
- end
+ it_behaves_like "contract is valid"
+ end
- describe "assigned_to_id" do
- before do
- work_package.assigned_to_id = other_user.id
- end
+ context "if the assigned user is not a possible assignee" do
+ before do
+ work_package.assigned_to = persisted_non_member
+ end
- context "if the assigned user is a possible assignee" do
- let(:possible_assignees) { [other_user] }
+ it_behaves_like "contract is invalid",
+ assigned_to: I18n.t("api_v3.errors.validation.invalid_user_assigned_to_work_package",
+ property: I18n.t("attributes.assignee"))
+ end
- it_behaves_like "has no error on", :assigned_to
- end
+ context "if the project is not set" do
+ before do
+ work_package.assigned_to = persisted_possible_assignee
+ work_package.project = nil
+ end
- context "if the assigned user is not a possible assignee" do
- it "is not a valid assignee" do
- error = I18n.t("api_v3.errors.validation.invalid_user_assigned_to_work_package",
- property: I18n.t("attributes.assignee"))
- expect(validated_contract.errors[:assigned_to]).to contain_exactly(error)
+ it_behaves_like "contract is invalid",
+ # But not on assignee
+ project: :blank
end
end
- context "if the project is not set" do
- let(:work_package_project) { nil }
+ describe "responsible_id" do
+ context "if the responsible user is a possible responsible" do
+ before do
+ work_package.responsible = persisted_possible_assignee
+ end
+
+ it_behaves_like "contract is valid"
+ end
+
+ context "if the assigned user is not a possible responsible" do
+ before do
+ work_package.responsible = persisted_non_member
+ end
+
+ it_behaves_like "contract is invalid",
+ responsible: I18n.t("api_v3.errors.validation.invalid_user_assigned_to_work_package",
+ property: I18n.t("attributes.responsible"))
+ end
+
+ context "if the project is not set" do
+ before do
+ work_package.responsible = persisted_possible_assignee
+ work_package.project = nil
+ end
- it_behaves_like "has no error on", :assigned_to
+ it_behaves_like "contract is invalid",
+ # But not on responsible
+ project: :blank
+ end
end
- end
- describe "responsible_id" do
- before do
- work_package.responsible_id = other_user.id
+ describe "remaining_hours" do
+ context "when is not a parent" do
+ context "when has not changed" do
+ it_behaves_like "contract is valid"
+ end
+
+ context "when has changed" do
+ before do
+ work_package.remaining_hours = 10
+ end
+
+ it_behaves_like "contract is valid"
+ end
+ end
end
- context "if the responsible user is a possible responsible" do
- let(:possible_assignees) { [other_user] }
+ describe "version" do
+ context "having full access" do
+ context "with an assignable_version" do
+ before do
+ work_package.version = persisted_project_version
+ end
+
+ it_behaves_like "contract is valid"
+ end
- it_behaves_like "has no error on", :responsible
+ context "with an unassignable_version" do
+ before do
+ work_package.version = persisted_other_project_version
+ end
+
+ it_behaves_like "contract is invalid", version_id: :inclusion
+ end
+ end
+
+ context "without the necessary permission to change versions" do
+ let(:permissions) { super() - %i[assign_versions] }
+
+ before do
+ work_package.version = persisted_project_version
+ end
+
+ it_behaves_like "contract is invalid", version_id: :error_readonly
+ end
end
- context "if the assigned user is not a possible responsible" do
- it "is not a valid responsible" do
- error = I18n.t("api_v3.errors.validation.invalid_user_assigned_to_work_package",
- property: I18n.t("attributes.responsible"))
- expect(validated_contract.errors[:responsible]).to contain_exactly(error)
+ describe "ignore_non_working_days" do
+ context "when not having children and scheduling manually" do
+ before do
+ work_package.ignore_non_working_days = !work_package.ignore_non_working_days
+ work_package.schedule_manually = true
+ end
+
+ it_behaves_like "contract is valid"
end
end
- context "if the project is not set" do
- let(:work_package_project) { nil }
+ describe "project_phase_definition", with_flag: { stages_and_gates: true } do
+ let(:permissions) { super() + %i[view_project_phases] }
+
+ before do
+ work_package.project_phase_definition = persisted_project_phase_definition
+ end
+
+ it_behaves_like "contract is valid"
+
+ context "without the necessary permission" do
+ let(:permissions) { super() - %i[view_project_phases] }
+
+ it_behaves_like "contract is invalid", project_phase_id: :error_readonly
+ end
+
+ context "without a project being set" do
+ before do
+ work_package.project = nil
+ end
- it_behaves_like "has no error on", :responsible
+ it_behaves_like "contract is invalid",
+ # But not on project_phase_id
+ project: :blank
+ end
+
+ context "when assigning a definition not active in the project" do
+ before do
+ work_package.project_phase_definition = persisted_inactive_project_phase.definition
+ end
+
+ it_behaves_like "contract is invalid", project_phase_id: :inclusion
+ end
+
+ context "with the feature being disabled", with_flag: { stages_and_gates: false } do
+ it_behaves_like "contract is invalid", project_phase_id: :error_readonly
+ end
end
end
- describe "#assignable_assignees" do
- it "returns the Principal`s possible_assignee scope" do
- expect(subject.assignable_assignees)
- .to eql assignable_assignees_scope
+ describe "#assignable_project_phases" do
+ context "when project is not set" do
+ before do
+ work_package.project = nil
+ end
+
+ it "returns an empty array" do
+ expect(contract.assignable_project_phases).to be_empty
+ end
end
- end
- describe "#assignable_responsibles" do
- it "returns the Principal`s possible_assignee scope" do
- expect(subject.assignable_responsibles)
- .to eql assignable_assignees_scope
+ context "when project is set" do
+ before do
+ work_package.project = persisted_project
+ end
+
+ it "returns the phases active in the project" do
+ expect(contract.assignable_project_phases).to contain_exactly(persisted_active_project_phase)
+ end
end
end
end
diff --git a/spec/contracts/work_packages/update_contract_spec.rb b/spec/contracts/work_packages/update_contract_spec.rb
index 874ee229fc3..6c4d0e9dc3f 100644
--- a/spec/contracts/work_packages/update_contract_spec.rb
+++ b/spec/contracts/work_packages/update_contract_spec.rb
@@ -29,324 +29,300 @@ require "spec_helper"
require "contracts/work_packages/shared_base_contract"
RSpec.describe WorkPackages::UpdateContract do
- shared_let(:persistent_project) { create(:project, public: false) }
+ include_context "work package contract"
+
shared_let(:type) { create(:type) }
- let(:work_package_project) { persistent_project }
- let(:work_package) do
- build_stubbed(:work_package,
- project: work_package_project,
- type:,
- status:) do |wp|
- wp_scope = double("wp scope")
-
- allow(WorkPackage)
- .to receive(:visible)
- .with(user)
- .and_return(wp_scope)
-
- allow(wp_scope)
- .to receive(:exists?) do |id|
- permissions.include?(:view_work_packages) && id == wp.id
- end
- end
+ shared_let(:persisted_work_package) do
+ create(:work_package,
+ project: persisted_project,
+ type: persisted_type,
+ status: persisted_status)
end
- let(:user) { build_stubbed(:user) }
- let(:status) { build_stubbed(:status) }
- let(:permissions) { %i[view_work_packages edit_work_packages assign_versions] }
-
- before do
- mock_permissions_for(user) do |mock|
- mock.allow_in_project(*permissions, project: work_package_project) if work_package_project
+ shared_let(:persisted_parent_work_package) do
+ create(:work_package,
+ project: persisted_project,
+ type: persisted_type,
+ status: persisted_status) do |parent|
+ create(:work_package,
+ parent:,
+ project: persisted_project,
+ type: persisted_type,
+ status: persisted_status)
end
end
- subject(:contract) { described_class.new(work_package, user) }
-
- it_behaves_like "work package contract" do
- let(:work_package) do
- build_stubbed(:work_package,
- project: work_package_project)
- end
+ let(:work_package) do
+ persisted_work_package
end
+ let(:user) { persisted_user }
+ let(:permissions) { %i[view_work_packages edit_work_packages assign_versions] }
- describe "lock_version" do
- context "no lock_version present" do
- before do
- work_package.lock_version = nil
- contract.validate
+ describe "validations" do
+ describe "general authorization" do
+ context "without read access" do
+ let(:permissions) { [:edit_work_packages] }
+
+ it_behaves_like "contract is invalid", base: :error_not_found
end
- it { expect(contract.errors.symbols_for(:base)).to include(:error_conflict) }
+ context "without write access" do
+ let(:permissions) { [:view_work_packages] }
+
+ it_behaves_like "contract user is unauthorized"
+ end
end
- context "lock_version changed" do
- before do
- work_package.lock_version += 1
- contract.validate
+ describe "lock_version" do
+ context "without a lock_version present" do
+ before do
+ work_package.lock_version = nil
+ end
+
+ it_behaves_like "contract is invalid", base: :error_conflict
end
- it { expect(contract.errors.symbols_for(:base)).to include(:error_conflict) }
- end
+ context "with the lock_version changed" do
+ before do
+ work_package.lock_version += 1
+ end
- context "lock_version present and unchanged" do
- before do
- contract.validate
+ it_behaves_like "contract is invalid", base: :error_conflict
end
- it { expect(contract.errors.symbols_for(:base)).not_to include(:error_conflict) }
+ context "with lock_version present and unchanged" do
+ it_behaves_like "contract is valid"
+ end
end
- end
- describe "authorization" do
- let(:attributes) { {} }
+ describe "project_id" do
+ let(:target_project) { other_persisted_project }
+ let(:target_permissions) { [:move_work_packages] }
- before do
- work_package.attributes = attributes
- contract.validate
- end
+ before do
+ mock_permissions_for(user) do |mock|
+ mock.allow_in_project *permissions, project: persisted_project
+ mock.allow_in_project *target_permissions, project: target_project
+ end
- context "full access" do
- it "is valid" do
- expect(contract.errors).to be_empty
+ work_package.project = target_project
end
- end
- context "no read access" do
- let(:permissions) { [:edit_work_packages] }
+ it_behaves_like "contract is valid"
- it { expect(contract.errors.symbols_for(:base)).to include(:error_not_found) }
- end
+ context "if the user lacks the permissions" do
+ let(:target_permissions) { [] }
- context "no write access" do
- let(:permissions) { [:view_work_packages] }
-
- it { expect(contract.errors.symbols_for(:base)).to include(:error_unauthorized) }
+ it_behaves_like "contract is invalid", project_id: :error_readonly
+ end
end
- context "only comment permission" do
- let(:permissions) { %i[view_work_packages add_work_package_notes] }
+ describe "remaining_hours" do
+ # not parent case covered by shared base contract
+ context "when is a parent" do
+ let(:work_package) { persisted_parent_work_package }
- context "when only adding a journal" do
- let(:attributes) { { journal_notes: "some notes" } }
-
- it "is valid" do
- expect(contract.errors).to be_empty
+ context "when has not changed" do
+ it_behaves_like "contract is valid"
end
- end
- context "when changing more than a journal" do
- let(:attributes) { { journal_notes: "some notes", subject: "blubs" } }
+ context "when has changed" do
+ before do
+ work_package.remaining_hours = 10
+ end
- it "is invalid" do
- expect(contract.errors.symbols_for(:base)).to include(:error_unauthorized)
+ it_behaves_like "contract is valid"
end
end
end
- context "only assign_versions permission" do
- let(:permissions) { %i[view_work_packages assign_versions] }
-
- it "is valid" do
- expect(contract.errors).to be_empty
- end
- end
- end
+ describe "ignore_non_working_days" do
+ context "when having children and not being scheduled manually" do
+ before do
+ allow(work_package)
+ .to receive(:leaf?)
+ .and_return(false)
- describe "project_id" do
- let(:target_project) { create(:project, types: [type]) }
- let(:target_permissions) { [:move_work_packages] }
+ work_package.ignore_non_working_days = !work_package.ignore_non_working_days
+ work_package.schedule_manually = false
+ end
- before do
- mock_permissions_for(user) do |mock|
- mock.allow_in_project *permissions, project: work_package_project
- mock.allow_in_project *target_permissions, project: target_project
+ it_behaves_like "contract is invalid", ignore_non_working_days: :error_readonly
end
- allow(work_package).to receive(:project) do
- if work_package.project_id == target_project.id
- target_project
- else
- work_package_project
- end
- end
+ context "when having children and being scheduled manually" do
+ before do
+ allow(work_package)
+ .to receive(:leaf?)
+ .and_return(false)
- work_package.project = target_project
+ work_package.ignore_non_working_days = !work_package.ignore_non_working_days
+ work_package.schedule_manually = true
+ end
- contract.validate
+ it_behaves_like "contract is valid"
+ end
end
- context "if the user has the permissions" do
- it("is valid") { expect(contract.errors).to be_empty }
- end
+ describe "type" do
+ let!(:milestone_type) do
+ create(:type, is_milestone: true, projects: [work_package.project])
+ end
- context "if the user lacks the permissions" do
- let(:target_permissions) { [] }
+ context "when changing to a milestone type and having a child work package" do
+ let!(:child_work_package) do
+ create(:work_package,
+ project: work_package.project,
+ parent: work_package)
+ end
+
+ before do
+ work_package.type = milestone_type
+ end
- it "is invalid" do
- expect(contract.errors.symbols_for(:project_id)).to contain_exactly(:error_readonly)
+ it_behaves_like "contract is invalid", type: :cannot_be_milestone_due_to_children
end
end
- end
-
- describe "version" do
- let(:version) { build_stubbed(:version) }
- before do
- allow(work_package)
- .to receive(:assignable_versions)
- .and_return([version])
+ describe "journal_notes" do
+ context "when only adding a comment and having only the comment permission permission" do
+ let(:permissions) { %i[view_work_packages add_work_package_notes] }
- work_package.attributes = attributes
+ before do
+ work_package.attributes = { journal_notes: "some notes" }
+ end
- contract.validate
- end
+ it_behaves_like "contract is valid"
+ end
- context "having full access" do
- context "with an assignable_version" do
- let(:attributes) { { version_id: version.id } }
+ context "when changing more than a comment and having only the comment permission permission" do
+ let(:permissions) { %i[view_work_packages add_work_package_notes] }
- it "is valid" do
- expect(contract.errors).to be_empty
+ before do
+ work_package.attributes = { journal_notes: "some notes", subject: "blubs" }
end
- end
- context "with an unassignable_version" do
- let(:attributes) { { version_id: version.id + 1 } }
+ it_behaves_like "contract user is unauthorized"
+ end
- it "adds an error" do
- expect(contract.errors.symbols_for(:version_id))
- .to include(:inclusion)
+ context "when only adding a comment and having edit permissions" do
+ before do
+ work_package.attributes = { journal_notes: "some notes" }
end
+
+ it_behaves_like "contract is valid"
end
end
- context "write access" do
- let(:permissions) { %i[view_work_packages edit_work_packages] }
-
- context "if assigning a version" do
- let(:attributes) { { version_id: version.id } }
+ describe "readonly status" do
+ context "with the status being readonly", with_ee: %i[readonly_work_packages] do
+ shared_let(:readonly_status) { create(:status, is_readonly: true) }
- it "adds an error" do
- expect(contract.errors.symbols_for(:version_id))
- .to include(:error_readonly)
+ before do
+ work_package.status = readonly_status
+ work_package.save
end
- end
- end
- end
- describe "ignore_non_working_days" do
- context "when having children and not being scheduled manually" do
- before do
- allow(work_package)
- .to receive(:leaf?)
- .and_return(false)
+ describe "updating the priority (representative for default attributes)" do
+ let(:new_priority) { build_stubbed(:priority) }
- work_package.ignore_non_working_days = !work_package.ignore_non_working_days
- work_package.schedule_manually = false
+ before do
+ work_package.priority = new_priority
- contract.validate
- end
+ contract.validate
+ end
- it "is invalid" do
- expect(contract.errors.symbols_for(:ignore_non_working_days))
- .to contain_exactly(:error_readonly)
- end
- end
+ it_behaves_like "contract is invalid",
+ priority_id: :error_readonly,
+ base: :readonly_status
+ end
- context "when having children and being scheduled manually" do
- before do
- allow(work_package)
- .to receive(:leaf?)
- .and_return(false)
+ describe "updating the custom field values" do
+ let(:cf1) { create(:string_wp_custom_field) }
- work_package.ignore_non_working_days = !work_package.ignore_non_working_days
- work_package.schedule_manually = true
+ before do
+ persisted_project.work_package_custom_fields << cf1
+ persisted_type.custom_fields << cf1
+ work_package.custom_field_values = { cf1.id => "test" }
+ contract.validate
+ end
- contract.validate
- end
+ shared_examples_for "custom_field readonly errors" do
+ it "adds an error to the written custom field attribute" do
+ expect(contract.errors.symbols_for(cf1.attribute_name.to_sym))
+ .to include(:error_readonly)
+ end
- it "is valid" do
- expect(contract.errors).to be_empty
- end
- end
- end
+ it "adds an error to base to better explain" do
+ expect(contract.errors.symbols_for(:base))
+ .to include(:readonly_status)
+ end
+ end
- describe "with children" do
- context "changing to milestone" do
- let(:milestone) { build_stubbed(:type, is_milestone: true) }
- let(:children) { [build_stubbed(:work_package)] }
+ context "when the subject does not extends OpenProject::ChangedBySystem" do
+ include_examples "custom_field readonly errors"
+ end
- before do
- work_package.type = milestone
- allow(work_package).to receive(:children).and_return children
- contract.validate
- end
+ context "when the subject extends OpenProject::ChangedBySystem" do
+ before do
+ work_package.extend(OpenProject::ChangedBySystem)
+ end
- it "adds an error because cannot change to milestone with children" do
- expect(contract.errors.symbols_for(:type)).to include(:cannot_be_milestone_due_to_children)
+ include_examples "custom_field readonly errors"
+ end
+ end
end
end
- end
- describe "parent_id" do
- shared_let(:parent) { create(:work_package) }
+ describe "parent_id" do
+ shared_let(:parent) { create(:work_package) }
- let(:parent_visible) { true }
-
- before do
- work_package.parent_id = parent.id
- allow(work_package.parent).to receive(:visible?).and_return(parent_visible)
- contract.validate
- end
+ let(:parent_visible) { true }
- context "if the user has only edit permissions" do
- it { expect(contract.errors.symbols_for(:parent_id)).to include(:error_readonly) }
- end
+ before do
+ work_package.parent = parent
- context "if the user has edit and subtasks permissions" do
- let(:permissions) { %i[edit_work_packages view_work_packages manage_subtasks] }
+ allow(parent)
+ .to receive(:visible?)
+ .and_return(parent_visible)
+ end
- it("is valid") do
- expect(contract.errors).to be_empty
+ context "if the user has only edit permissions" do
+ it_behaves_like "contract is invalid", parent_id: :error_readonly
end
- describe "invalid lock version" do
- before do
- work_package.lock_version = 9999
- contract.validate
- end
+ context "if the user has edit and subtasks permissions" do
+ let(:permissions) { %i[edit_work_packages view_work_packages manage_subtasks] }
- it { expect(contract.errors.symbols_for(:base)).to include(:error_conflict) }
+ it_behaves_like "contract is valid"
end
- end
- context "no write access" do
- let(:permissions) { [:view_work_packages] }
+ context "if the user lacks all edit and subtask permissions" do
+ let(:permissions) { [:view_work_packages] }
- it { expect(contract.errors.symbols_for(:parent_id)).to include(:error_readonly) }
- end
+ it_behaves_like "contract is invalid", parent_id: :error_readonly
+ end
- context "with manage_subtasks permission" do
- let(:permissions) { %i[view_work_packages manage_subtasks] }
+ context "with manage_subtasks permission" do
+ let(:permissions) { %i[view_work_packages manage_subtasks] }
- it("is valid") do
- expect(contract.errors).to be_empty
- end
+ it_behaves_like "contract is valid"
- describe "changing more than the parent_id" do
- before do
- work_package.subject = "Foobar!"
- contract.validate
- end
+ describe "changing more than the parent_id" do
+ before do
+ work_package.subject = "Foobar!"
+ end
- it { expect(contract.errors.symbols_for(:subject)).to include(:error_readonly) }
+ it_behaves_like "contract is invalid", subject: :error_readonly
+ end
end
- end
- context "when the user does not have access to the parent" do
- let(:parent_visible) { false }
+ context "when the user has the necessary permission on the work package but does not have access to the parent" do
+ let(:permissions) { %i[view_work_packages manage_subtasks] }
- it { expect(contract.errors.symbols_for(:parent_id)).to include(:error_unauthorized) }
+ let(:parent_visible) { false }
+
+ it_behaves_like "contract is invalid", parent_id: %i[error_unauthorized]
+ end
end
end
@@ -378,127 +354,17 @@ RSpec.describe WorkPackages::UpdateContract do
end
end
- describe "readonly status" do
- context "with the status being readonly", with_ee: %i[readonly_work_packages] do
- let(:status) { build_stubbed(:status, is_readonly: true) }
-
- describe "updating the priority" do
- let(:new_priority) { build_stubbed(:priority) }
-
- before do
- work_package.priority = new_priority
-
- contract.validate
- end
-
- it "is invalid" do
- expect(contract)
- .not_to be_valid
- end
-
- it "adds an error to the written to attribute" do
- expect(contract.errors.symbols_for(:priority_id))
- .to include(:error_readonly)
- end
-
- it "adds an error to base to better explain" do
- expect(contract.errors.symbols_for(:base))
- .to include(:readonly_status)
- end
- end
-
- describe "updating the custom field values" do
- let(:cf1) { create(:string_wp_custom_field) }
-
- before do
- work_package_project.work_package_custom_fields << cf1
- type.custom_fields << cf1
- work_package.custom_field_values = { cf1.id => "test" }
- contract.validate
- end
-
- shared_examples_for "custom_field readonly errors" do
- it "adds an error to the written custom field attribute" do
- expect(contract.errors.symbols_for(cf1.attribute_name.to_sym))
- .to include(:error_readonly)
- end
-
- it "adds an error to base to better explain" do
- expect(contract.errors.symbols_for(:base))
- .to include(:readonly_status)
- end
- end
-
- context "when the subject does not extends OpenProject::ChangedBySystem" do
- include_examples "custom_field readonly errors"
- end
-
- context "when the subject extends OpenProject::ChangedBySystem" do
- before do
- work_package.extend(OpenProject::ChangedBySystem)
- end
-
- include_examples "custom_field readonly errors"
- end
- end
+ describe "#assignable_assignees" do
+ it "returns the users assignable" do
+ expect(subject.assignable_assignees)
+ .to contain_exactly(persisted_possible_assignee)
end
end
- describe "remaining hours" do
- context "when is not a parent" do
- before do
- contract.validate
- end
-
- context "when has not changed" do
- it("is valid") { expect(contract.errors.empty?).to be true }
- end
-
- context "when has changed" do
- before do
- work_package.remaining_hours = 10
- end
-
- it("is valid") { expect(contract.errors.empty?).to be true }
- end
- end
-
- context "when is a parent" do
- let(:work_package) do
- build_stubbed(:work_package,
- project: work_package_project,
- children: [build_stubbed(:work_package, project: work_package_project)],
- type:,
- status:) do |wp|
- wp_scope = instance_double(ActiveRecord::Relation)
-
- allow(WorkPackage)
- .to receive(:visible)
- .with(user)
- .and_return(wp_scope)
-
- allow(wp_scope)
- .to receive(:exists?) do |id|
- permissions.include?(:view_work_packages) && id == wp.id
- end
- end
- end
-
- before do
- contract.validate
- end
-
- context "when has not changed" do
- it("is valid") { expect(contract.errors.empty?).to be true }
- end
-
- context "when has changed" do
- before do
- work_package.remaining_hours = 10
- end
-
- it("is valid") { expect(contract.errors.empty?).to be true }
- end
+ describe "#assignable_responsibles" do
+ it "returns the users assignable" do
+ expect(subject.assignable_responsibles)
+ .to contain_exactly(persisted_possible_assignee)
end
end
end
diff --git a/spec/factories/project_phase_factory.rb b/spec/factories/project_phase_factory.rb
index bf5e600d07d..f8c520eb48b 100644
--- a/spec/factories/project_phase_factory.rb
+++ b/spec/factories/project_phase_factory.rb
@@ -44,5 +44,13 @@ FactoryBot.define do
trait :with_gated_definition do
definition { association(:project_phase_definition, :with_start_gate, :with_finish_gate) }
end
+
+ trait :active do
+ active { true }
+ end
+
+ trait :inactive do
+ active { false }
+ end
end
end
diff --git a/spec/features/work_packages/project_phases/linking_spec.rb b/spec/features/work_packages/project_phases/linking_spec.rb
index ee4885ff58c..80dbcdce513 100644
--- a/spec/features/work_packages/project_phases/linking_spec.rb
+++ b/spec/features/work_packages/project_phases/linking_spec.rb
@@ -54,8 +54,12 @@ RSpec.describe "Linking Projects phases and work packages settings", :js, with_f
it "allows seeing and editing linked phases" do
work_package_page.visit!
- work_package_page.expect_attributes(project_phase_definition: executing_phase_definition.name)
+ work_package_page.expect_attributes(project_phase: executing_phase_definition.name)
- work_package_page.set_attributes({ project_phase_definition: initiating_phase_definition.name })
+ work_package_page.set_attributes({ projectPhase: initiating_phase_definition.name })
+
+ work_package_page.expect_and_dismiss_toaster(message: "Successful update.")
+
+ work_package_page.expect_attributes(project_phase: initiating_phase_definition.name)
end
end
diff --git a/spec/lib/api/v3/work_packages/schema/specific_work_package_schema_spec.rb b/spec/lib/api/v3/work_packages/schema/specific_work_package_schema_spec.rb
index 6008b134cf4..20a88a8ed02 100644
--- a/spec/lib/api/v3/work_packages/schema/specific_work_package_schema_spec.rb
+++ b/spec/lib/api/v3/work_packages/schema/specific_work_package_schema_spec.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -36,15 +38,36 @@ RSpec.describe API::V3::WorkPackages::Schema::SpecificWorkPackageSchema do
project:,
type:)
end
- let(:current_user) { build_stubbed(:user) }
+ let(:contract_class) { WorkPackages::UpdateContract }
+ let(:contract) { instance_double(contract_class) }
before do
mock_permissions_for(current_user, &:allow_everything)
- login_as(current_user)
end
+ current_user { build_stubbed(:user) }
+
subject { described_class.new(work_package:) }
+ shared_examples_for "assignable values" do
+ before do
+ allow(contract_class)
+ .to receive(:new)
+ .with(work_package, current_user)
+ .and_return(contract)
+ end
+
+ it "delegates to the contract" do
+ allow(contract)
+ .to receive(assignable_method)
+
+ subject.send(assignable_method)
+
+ expect(contract)
+ .to have_received(assignable_method)
+ end
+ end
+
it "has the project set" do
expect(subject.project).to eql(project)
end
@@ -105,65 +128,58 @@ RSpec.describe API::V3::WorkPackages::Schema::SpecificWorkPackageSchema do
end
describe "#assignable_types" do
- let(:result) do
- result = double
- allow(result).to receive(:includes).and_return(result)
- result
- end
-
- it "calls through to the project" do
- expect(project).to receive(:types).and_return(result)
- expect(subject.assignable_values(:type, current_user)).to eql(result)
+ it_behaves_like "assignable values" do
+ let(:assignable_method) { :assignable_types }
end
end
describe "#assignable_versions" do
- let(:result) { double }
-
- it "calls through to the work package" do
- expect(work_package).to receive(:assignable_versions).and_return(result)
- expect(subject.assignable_values(:version, current_user)).to eql(result)
+ it_behaves_like "assignable values" do
+ let(:assignable_method) { :assignable_versions }
end
end
describe "#assignable_priorities" do
- let(:active_priority) { build(:priority, active: true) }
- let(:inactive_priority) { build(:priority, active: false) }
-
- before do
- active_priority.save!
- inactive_priority.save!
+ it_behaves_like "assignable values" do
+ let(:assignable_method) { :assignable_priorities }
end
+ end
- it "returns only active priorities" do
- expect(subject.assignable_values(:priority, current_user).size).to be >= 1
- subject.assignable_values(:priority, current_user).each do |priority|
- expect(priority.active).to be_truthy
- end
+ describe "#assignable_categories" do
+ it_behaves_like "assignable values" do
+ let(:assignable_method) { :assignable_categories }
end
end
- describe "#assignable_categories" do
- let(:category) { instance_double(Category) }
+ describe "#assignable_custom_field_values" do
+ let(:list_cf) { create(:list_wp_custom_field) }
+ let(:version_cf) { build_stubbed(:version_wp_custom_field) }
- before do
- allow(project).to receive(:categories).and_return([category])
+ it "is a list custom fields' possible values" do
+ expect(subject.assignable_custom_field_values(list_cf))
+ .to match_array list_cf.possible_values
end
- it "returns all categories of the project" do
- expect(subject.assignable_values(:category, current_user)).to contain_exactly(category)
+ it "is a version custom fields' project values" do
+ result = [instance_double(Version)]
+
+ allow(work_package)
+ .to receive(:assignable_versions)
+ .and_return(result)
+
+ expect(subject.assignable_custom_field_values(version_cf)).to eql result
end
end
- describe "#assignable_budgets" do
- subject { described_class.new(work_package:) }
-
- before do
- allow(project).to receive(:budgets).and_return([instance_double(Budget)])
+ describe "#assignable_project_phases" do
+ it_behaves_like "assignable values" do
+ let(:assignable_method) { :assignable_project_phases }
end
+ end
- it "returns project.budgets" do
- expect(subject.assignable_values(:budget, nil)).to eql(project.budgets)
+ describe "#assignable_budgets" do
+ it_behaves_like "assignable values" do
+ let(:assignable_method) { :assignable_budgets }
end
end
@@ -325,24 +341,4 @@ RSpec.describe API::V3::WorkPackages::Schema::SpecificWorkPackageSchema do
end
end
end
-
- describe "#assignable_custom_field_values" do
- let(:list_cf) { create(:list_wp_custom_field) }
- let(:version_cf) { build_stubbed(:version_wp_custom_field) }
-
- it "is a list custom fields' possible values" do
- expect(subject.assignable_custom_field_values(list_cf))
- .to match_array list_cf.possible_values
- end
-
- it "is a version custom fields' project values" do
- result = [instance_double(Version)]
-
- allow(work_package)
- .to receive(:assignable_versions)
- .and_return(result)
-
- expect(subject.assignable_custom_field_values(version_cf)).to eql result
- end
- end
end
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 363ec9bbf82..d78c9ae14e5 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
@@ -825,35 +825,37 @@ RSpec.describe API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
end
end
- describe "projectPhaseDefinition" do
- # TODO: spec feature flag
- context "with the view_project_phases permission" do
- let(:permissions) { %i[edit_work_packages view_project_phases] }
+ describe "projectPhase" do
+ context "with the feature enabled", with_flag: { stages_and_gates: true } do
+ context "with the view_project_phases permission" do
+ let(:permissions) { %i[edit_work_packages view_project_phases] }
+
+ it_behaves_like "has basic schema properties" do
+ let(:path) { "projectPhase" }
+ let(:type) { "ProjectPhase" }
+ let(:name) { I18n.t("attributes.project_phase") }
+ let(:required) { false }
+ let(:writable) { true }
+ let(:location) { "_links" }
+ end
- it_behaves_like "has basic schema properties" do
- let(:path) { "projectPhase" }
- let(:type) { "ProjectPhase" }
- let(:name) { I18n.t("attributes.project_phase") }
- let(:required) { false }
- let(:writable) { false }
- let(:location) { "_links" }
+ it_behaves_like "has a collection of allowed values" do
+ let(:json_path) { "projectPhase" }
+ let(:href_path) { "project_phases" }
+ let(:factory) { :project_phase }
+ end
end
- end
- context "with the edit_project_phases permission" do
- let(:permissions) { %i[edit_work_packages view_project_phases edit_project_phases] }
-
- it_behaves_like "has basic schema properties" do
- let(:path) { "projectPhase" }
- let(:type) { "ProjectPhase" }
- let(:name) { I18n.t("attributes.project_phase") }
- let(:required) { false }
- let(:writable) { true }
- let(:location) { "_links" }
+ context "without the view_project_phases permission" do
+ it "has no projectPhase attribute" do
+ expect(subject).not_to have_json_path("projectPhase")
+ end
end
end
- context "without the view_project_phases permission" do
+ context "with the feature disabled" do
+ let(:permissions) { %i[edit_work_packages view_project_phases] }
+
it "has no projectPhase attribute" do
expect(subject).not_to have_json_path("projectPhase")
end
diff --git a/spec/lib/api/v3/work_packages/work_package_payload_representer_parsing_spec.rb b/spec/lib/api/v3/work_packages/work_package_payload_representer_parsing_spec.rb
new file mode 100644
index 00000000000..580678b896e
--- /dev/null
+++ b/spec/lib/api/v3/work_packages/work_package_payload_representer_parsing_spec.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.
+# ++
+
+require "spec_helper"
+
+RSpec.describe API::V3::WorkPackages::WorkPackagePayloadRepresenter, "parsing" do
+ include API::V3::Utilities::PathHelper
+
+ let(:member) { build_stubbed(:user) }
+ let(:hash) { {} }
+ let(:current_user) { member }
+ let(:representer) do
+ described_class.create(API::V3::WorkPackages::ParseParamsService::ParsingStruct.new, current_user:)
+ end
+ let(:duration) { nil }
+
+ subject { representer.from_hash(hash.stringify_keys) }
+
+ describe "duration" do
+ let(:hash) do
+ {
+ duration: "P6D"
+ }
+ end
+
+ it "parses from iso8601 format" do
+ expect(subject.duration).to eq(6)
+ end
+ end
+
+ describe "project_phase" do
+ let(:project) { build_stubbed(:project_with_types, phases: project_phase.present? ? [project_phase] : []) }
+ let(:project_phase) { build_stubbed(:project_phase, definition: project_phase_definition) }
+ let(:project_phase_definition) { build_stubbed(:project_phase_definition) }
+
+ let(:hash) do
+ {
+ _links: {
+ "projectPhase" => {
+ "href" => api_v3_paths.project_phase(project_phase.id)
+ },
+ "project" => {
+ "href" => api_v3_paths.project(project.id)
+ }
+ }
+ }
+ end
+
+ before do
+ phase_scope = instance_double(ActiveRecord::Relation)
+ allow(Project::Phase)
+ .to receive(:where)
+ .with(id: project_phase.id.to_s)
+ .and_return(phase_scope)
+
+ allow(phase_scope)
+ .to receive(:pick)
+ .with(:definition_id)
+ .and_return(project_phase_definition.id)
+ end
+
+ it "parses to the project_phase_definition_id" do
+ expect(subject.project_phase_definition_id).to eq(project_phase_definition.id.to_s)
+ end
+ end
+end
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 f0ea986733f..c1943c16344 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
@@ -675,43 +675,51 @@ RSpec.describe API::V3::WorkPackages::WorkPackageRepresenter do
end
describe "projectPhase" do
- context "with a phase being set" do
- it_behaves_like "has a titled link" do
- let(:link) { "projectPhase" }
- let(:href) { api_v3_paths.project_phase(project_phase.id) }
- let(:title) { project_phase.name }
+ context "with the feature flag being active", with_flag: { stages_and_gates: true } do
+ context "with a phase being set" do
+ it_behaves_like "has a titled link" do
+ let(:link) { "projectPhase" }
+ let(:href) { api_v3_paths.project_phase(project_phase.id) }
+ let(:title) { project_phase.name }
+ end
end
- end
- context "without a phase being set" do
- before do
- work_package.project_phase_definition = nil
+ context "without a phase being set" do
+ before do
+ work_package.project_phase_definition = nil
+ end
+
+ it_behaves_like "has an empty link" do
+ let(:link) { "projectPhase" }
+ end
end
- it_behaves_like "has an empty link" do
- let(:link) { "projectPhase" }
+ context "with the phase not existing in the project" do
+ let(:project_phase) { nil }
+
+ it_behaves_like "has an empty link" do
+ let(:link) { "projectPhase" }
+ end
end
- end
- context "with the phase not existing in the project" do
- let(:project_phase) { nil }
+ context "with the phase being inactive in the project" do
+ let(:project_phase) { build_stubbed(:project_phase, active: false, definition: project_phase_definition) }
- it_behaves_like "has an empty link" do
- let(:link) { "projectPhase" }
+ it_behaves_like "has an empty link" do
+ let(:link) { "projectPhase" }
+ end
end
- end
- context "with the phase being inactive in the project" do
- let(:project_phase) { build_stubbed(:project_phase, active: false, definition: project_phase_definition) }
+ context "without the user being allowed to see the reference" do
+ let(:permissions) { all_permissions - [:view_project_phases] }
- it_behaves_like "has an empty link" do
- let(:link) { "projectPhase" }
+ it_behaves_like "has no link" do
+ let(:link) { "projectPhase" }
+ end
end
end
- context "without the user being allowed to see the reference" do
- let(:permissions) { all_permissions - [:view_project_phases] }
-
+ context "without the feature flag being active" do
it_behaves_like "has no link" do
let(:link) { "projectPhase" }
end
@@ -1367,33 +1375,39 @@ RSpec.describe API::V3::WorkPackages::WorkPackageRepresenter do
let(:embedded_resource) { project_phase_definition }
let(:embedded_resource_type) { "ProjectPhase" }
- context "with a phase being set" do
- it_behaves_like "has the resource embedded"
- end
+ context "with the feature flag being active", with_flag: { stages_and_gates: true } do
+ context "with a phase being set" do
+ it_behaves_like "has the resource embedded"
+ end
- context "without a phase being set" do
- before do
- work_package.project_phase_definition = nil
+ context "without a phase being set" do
+ before do
+ work_package.project_phase_definition = nil
+ end
+
+ it_behaves_like "has the resource not embedded"
end
- it_behaves_like "has the resource not embedded"
- end
+ context "with the phase not existing in the project" do
+ let(:project_phase) { nil }
- context "with the phase not existing in the project" do
- let(:project_phase) { nil }
+ it_behaves_like "has the resource not embedded"
+ end
- it_behaves_like "has the resource not embedded"
- end
+ context "with the phase being inactive in the project" do
+ let(:project_phase) { build_stubbed(:project_phase, active: false, definition: project_phase_definition) }
- context "with the phase being inactive in the project" do
- let(:project_phase) { build_stubbed(:project_phase, active: false, definition: project_phase_definition) }
+ it_behaves_like "has the resource not embedded"
+ end
- it_behaves_like "has the resource not embedded"
- end
+ context "without the user being allowed to see the reference" do
+ let(:permissions) { all_permissions - [:view_project_phases] }
- context "without the user being allowed to see the reference" do
- let(:permissions) { all_permissions - [:view_project_phases] }
+ it_behaves_like "has the resource not embedded"
+ end
+ end
+ context "with the feature flag being inactive" do
it_behaves_like "has the resource not embedded"
end
end
diff --git a/spec/support/edit_fields/edit_field.rb b/spec/support/edit_fields/edit_field.rb
index 9b091522d3e..2645fa25424 100644
--- a/spec/support/edit_fields/edit_field.rb
+++ b/spec/support/edit_fields/edit_field.rb
@@ -26,7 +26,6 @@ class EditField
property_name,
selector: nil,
create_form: false)
-
@property_name = property_name.to_s
@context = context
@field_type = derive_field_type
@@ -308,7 +307,7 @@ class EditField
"version-autocompleter"
when :assignee, :responsible, :user
"op-user-autocompleter"
- when :priority, :status, :type, :category, :workPackage, :parent
+ when :priority, :status, :type, :category, :workPackage, :parent, :projectPhase
"create-autocompleter"
when :project
"op-project-autocompleter"