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

consolidate allowed_* logic in the wp contract

ulferts6c4e572f4e8a
diff --git a/app/contracts/work_packages/base_contract.rb b/app/contracts/work_packages/base_contract.rb
index 8b06202a02a..2291699355e 100644
--- a/app/contracts/work_packages/base_contract.rb
+++ b/app/contracts/work_packages/base_contract.rb
@@ -33,6 +33,7 @@ require 'model_contract'
module WorkPackages
class BaseContract < ::ModelContract
include ::Attachments::ValidateReplacements
+ include ::Concerns::AssignableValuesContract
attribute :subject
attribute :description
@@ -40,7 +41,7 @@ module WorkPackages
writeable: ->(*) {
# If we did not change into the status,
# mark unwritable if status and version is closed
- model.status_id_change || !model.closed_version_and_status?
+ model.status_id_change || !closed_version_and_status?
}
attribute :type_id
attribute :priority_id
@@ -143,6 +144,41 @@ module WorkPackages
ret
end
+ def assignable_statuses
+ # Do not allow skipping statuses without intermediately saving the work package.
+ # We therefore take the original status of the work_package, while preserving all
+ # other changes to it (e.g. type, assignee, etc.)
+ status = if model.persisted? && model.status_id_changed?
+ Status.find_by(id: model.status_id_was)
+ else
+ model.status
+ end
+
+ new_statuses_allowed_from(status)
+ end
+
+ def assignable_types
+ scope = if model.project.nil?
+ Type
+ else
+ model.project.types.includes(:color)
+ end
+
+ scope.includes(:color)
+ end
+
+ def assignable_categories
+ model.project.categories if model.project.respond_to?(:categories)
+ end
+
+ def assignable_priorities
+ IssuePriority.active
+ end
+
+ def assignable_versions
+ model.try(:assignable_versions) if model.project
+ end
+
private
attr_reader :can
@@ -286,9 +322,7 @@ module WorkPackages
end
def status_transition_exists?
- model.type.valid_transition?(model.status_id_was,
- model.status_id,
- user.roles(model.project))
+ assignable_statuses.exists?(model.status_id)
end
def invalid_relations_with_new_hierarchy
@@ -317,5 +351,48 @@ module WorkPackages
def type_inexistent?
model.type.is_a?(Type::InexistentType)
end
+
+ # Returns a scope of status the user is able to apply
+ def new_statuses_allowed_from(status)
+ return Status.where('1=0') if status.nil?
+
+ current_status = Status.where(id: status.id)
+
+ return current_status if closed_version_and_status?(status)
+
+ statuses = new_statuses_by_workflow(status)
+ .or(current_status)
+
+ statuses = statuses.where(is_closed: false) if model.blocked?
+
+ statuses.order_by_position
+ end
+
+ def closed_version_and_status?(status = model.status)
+ model.fixed_version&.closed? && status.is_closed?
+ end
+
+ def new_statuses_by_workflow(status)
+ workflows = Workflow
+ .from_status(status.id,
+ model.type_id,
+ users_roles_in_project.map(&:id),
+ user_is_author?,
+ user_was_or_is_assignee?)
+
+ Status.where(id: workflows.select(:new_status_id))
+ end
+
+ def user_was_or_is_assignee?
+ model.assigned_to_id_changed? ? model.assigned_to_id_was == user.id : model.assigned_to_id == user.id
+ end
+
+ def user_is_author?
+ model.author == user
+ end
+
+ def users_roles_in_project
+ user.roles_for_project(model.project)
+ end
end
end
diff --git a/app/models/status.rb b/app/models/status.rb
index 2c433908a50..24427e5d9e1 100644
--- a/app/models/status.rb
+++ b/app/models/status.rb
@@ -75,28 +75,6 @@ class Status < ActiveRecord::Base
WorkPackage.use_status_for_done_ratio?
end
- # Returns an array of all statuses the given role can switch to
- def new_statuses_allowed_to(roles, type, author = false, assignee = false)
- self.class.new_statuses_allowed(self, roles, type, author, assignee)
- end
-
- def self.new_statuses_allowed(status, roles, type, author = false, assignee = false)
- if roles.present? && type.present?
- status_id = status.try(:id) || 0
-
- workflows = Workflow
- .from_status(status_id,
- type.id,
- roles.map(&:id),
- author,
- assignee)
-
- Status.where(id: workflows.select(:new_status_id))
- else
- Status.where('1 = 0')
- end
- end
-
def self.order_by_position
order(:position)
end
diff --git a/app/models/type.rb b/app/models/type.rb
index b923fea3d07..29265e6ac80 100644
--- a/app/models/type.rb
+++ b/app/models/type.rb
@@ -113,21 +113,9 @@ class ::Type < ActiveRecord::Base
object.types.include?(self)
end
- def valid_transition?(status_id_a, status_id_b, roles)
- transition_exists?(status_id_a, status_id_b, roles.map(&:id))
- end
-
private
def check_integrity
raise "Can't delete type" if WorkPackage.where(type_id: id).any?
end
-
- def transition_exists?(status_id_a, status_id_b, role_ids)
- workflows
- .where(old_status_id: status_id_a,
- new_status_id: status_id_b,
- role_id: role_ids)
- .any?
- end
end
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index 22e71c453d6..84590d3cb89 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -62,7 +62,7 @@ class WorkPackage < ActiveRecord::Base
order("#{Changeset.table_name}.committed_on ASC, #{Changeset.table_name}.id ASC")
}
- scope :recently_updated, ->() {
+ scope :recently_updated, -> {
order(updated_at: :desc)
}
@@ -84,7 +84,7 @@ class WorkPackage < ActiveRecord::Base
end
}
- scope :with_status_open, ->() {
+ scope :with_status_open, -> {
includes(:status)
.where(statuses: { is_closed: false })
}
@@ -283,10 +283,6 @@ class WorkPackage < ActiveRecord::Base
status.present? && status.is_readonly?
end
- def closed_version_and_status?
- fixed_version&.closed? && status.is_closed?
- end
-
# Returns true if the work_package is overdue
def overdue?
!due_date.nil? && (due_date < Date.today) && !closed?
@@ -297,23 +293,6 @@ class WorkPackage < ActiveRecord::Base
end
alias_method :is_milestone?, :milestone?
- # Returns an array of status that user is able to apply
- def new_statuses_allowed_to(user, include_default = false)
- return Status.where('1=0') if status.nil?
-
- current_status = Status.where(id: status_id)
-
- return current_status if closed_version_and_status?
-
- statuses = new_statuses_allowed_by_workflow_to(user)
- .or(current_status)
-
- statuses = statuses.or(Status.where_default) if include_default
- statuses = statuses.where(is_closed: false) if blocked?
-
- statuses.order_by_position
- end
-
# Returns users that should be notified
def recipients
notified = project.notified_users + attribute_users.select { |u| u.notify_about?(self) }
@@ -643,15 +622,6 @@ class WorkPackage < ActiveRecord::Base
time_entries.build(attributes)
end
- def new_statuses_allowed_by_workflow_to(user)
- status.new_statuses_allowed_to(
- user.roles_for_project(project),
- type,
- author == user,
- assigned_to_id_changed? ? assigned_to_id_was == user.id : assigned_to_id == user.id
- )
- end
-
##
# Checks if the time entry defined by the given attributes is blank.
# A time entry counts as blank despite a selected activity if that activity
diff --git a/app/services/work_packages/set_attributes_service.rb b/app/services/work_packages/set_attributes_service.rb
index 6a874320e7c..5628a5785c2 100644
--- a/app/services/work_packages/set_attributes_service.rb
+++ b/app/services/work_packages/set_attributes_service.rb
@@ -173,7 +173,7 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
work_package.type = available_types.detect(&:is_default) || available_types.first
- reassign_status work_package.new_statuses_allowed_to(user, true)
+ reassign_status assignable_statuses
end
def reassign_status(available_statuses)
@@ -214,4 +214,8 @@ class WorkPackages::SetAttributesService < ::BaseServices::SetAttributes
def work_package
model
end
+
+ def assignable_statuses
+ instantiate_contract(work_package, user).assignable_statuses.or(Status.where_default.order_by_position)
+ end
end
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 043da90e802..e38dec86e9a 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
@@ -48,6 +48,13 @@ module API
:available_custom_fields,
to: :@work_package
+ delegate :assignable_types,
+ :assignable_statuses,
+ :assignable_categories,
+ :assignable_priorities,
+ :assignable_versions,
+ to: :contract
+
def no_caching?
true
end
@@ -67,40 +74,6 @@ module API
User.current)
end
end
-
- def assignable_categories
- project.categories if project.respond_to?(:categories)
- end
-
- def assignable_priorities
- IssuePriority.active
- end
-
- def assignable_versions
- @work_package.try(:assignable_versions) if project
- end
-
- def assignable_types
- if project.nil?
- Type.includes(:color)
- else
- project.types.includes(:color)
- end
- end
-
- def assignable_statuses
- status_origin = @work_package
-
- # do not allow to skip statuses without intermediately saving the work package
- # we therefore take the original status of the work_package, while preserving all
- # other changes to it (e.g. type, assignee, etc.)
- if @work_package.persisted? && @work_package.status_id_changed?
- status_origin = @work_package.clone
- status_origin.status = Status.find_by(id: @work_package.status_id_was)
- end
-
- status_origin.new_statuses_allowed_to(User.current)
- end
end
end
end
diff --git a/lib/api/v3/work_packages/schema/typed_schema_contract.rb b/lib/api/v3/work_packages/schema/typed_schema_contract.rb
index 019fa53ea6f..16aa50847e2 100644
--- a/lib/api/v3/work_packages/schema/typed_schema_contract.rb
+++ b/lib/api/v3/work_packages/schema/typed_schema_contract.rb
@@ -29,7 +29,7 @@
#++
# The contract is not actually used for validations but rather to display the unimbedded schema
-# as the writable attributes differ depdending on whether the user has the necessary permissions.
+# as the writable attributes differ depending on whether the user has the necessary permissions.
# As we do not know the context of the schema, other than when it is embedded inside a form, we have to allow
# both possible permissions.
module API
diff --git a/modules/backlogs/app/controllers/work_package_boxes_controller.rb b/modules/backlogs/app/controllers/work_package_boxes_controller.rb
index b1bb24c5098..fcac7b98081 100644
--- a/modules/backlogs/app/controllers/work_package_boxes_controller.rb
+++ b/modules/backlogs/app/controllers/work_package_boxes_controller.rb
@@ -43,7 +43,7 @@ class WorkPackageBoxesController < WorkPackagesController
@changesets = @work_package.changesets.visible.all
@changesets.reverse! if User.current.wants_comments_in_reverse_order?
@relations = @work_package.relations.select { |r| r.other_work_package(@work_package) && r.other_work_package(@work_package).visible? }
- @allowed_statuses = @work_package.new_statuses_allowed_to(User.current)
+ @allowed_statuses = WorkPackages::UpdateContract.new(work_package, User.current).assignable_statuses
@edit_allowed = User.current.allowed_to?(:edit_work_packages, @project)
@priorities = IssuePriority.all
@time_entry = TimeEntry.new
diff --git a/modules/bcf/app/services/bcf/issues/delete_service.rb b/modules/bcf/app/services/bcf/issues/delete_service.rb
index ccfece7acfb..1cf70b99a29 100644
--- a/modules/bcf/app/services/bcf/issues/delete_service.rb
+++ b/modules/bcf/app/services/bcf/issues/delete_service.rb
@@ -34,6 +34,10 @@ module Bcf::Issues
def before_perform(params)
associated_wp = WorkPackage.find(model.work_package_id)
+ # Load the project association as AR fails do do so once the work package
+ # is destroyed.
+ model.project
+
wp_call = WorkPackages::DeleteService
.new(user: user,
model: associated_wp.reload)
diff --git a/modules/bcf/spec/contracts/bcf/work_packages/update_contract_spec.rb b/modules/bcf/spec/contracts/bcf/work_packages/update_contract_spec.rb
index e260e825375..c81b0bef060 100644
--- a/modules/bcf/spec/contracts/bcf/work_packages/update_contract_spec.rb
+++ b/modules/bcf/spec/contracts/bcf/work_packages/update_contract_spec.rb
@@ -1,4 +1,5 @@
#-- encoding: UTF-8
+
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
diff --git a/modules/bcf/spec/requests/api/bcf/v2_1/topics_api_spec.rb b/modules/bcf/spec/requests/api/bcf/v2_1/topics_api_spec.rb
index 2ae78071172..68af3ac0695 100644
--- a/modules/bcf/spec/requests/api/bcf/v2_1/topics_api_spec.rb
+++ b/modules/bcf/spec/requests/api/bcf/v2_1/topics_api_spec.rb
@@ -48,13 +48,17 @@ describe 'BCF 2.1 topics resource', type: :request, content_type: :json, with_ma
let(:edit_member_user) do
FactoryBot.create(:user,
member_in_project: project,
- member_with_permissions: %i[manage_bcf add_work_packages view_linked_issues delete_work_packages])
+ member_with_permissions: %i[manage_bcf
+ add_work_packages
+ view_linked_issues
+ view_work_packages
+ edit_work_packages
+ delete_work_packages])
end
let(:edit_work_package_member_user) do
FactoryBot.create(:user,
member_in_project: project,
- member_with_permissions: %i[manage_bcf
- add_work_packages
+ member_with_permissions: %i[add_work_packages
view_linked_issues
edit_work_packages
view_work_packages
diff --git a/spec/contracts/work_packages/base_contract_spec.rb b/spec/contracts/work_packages/base_contract_spec.rb
index a5c100f7e6b..ef7c9a18be4 100644
--- a/spec/contracts/work_packages/base_contract_spec.rb
+++ b/spec/contracts/work_packages/base_contract_spec.rb
@@ -161,9 +161,12 @@ describe WorkPackages::BaseContract do
context 'work_package has a closed version and status' do
before do
- allow(work_package)
- .to receive(:closed_version_and_status?)
- .and_return true
+ version = FactoryBot.build_stubbed(:version, status: 'closed')
+
+ work_package.fixed_version = version
+ allow(work_package.status)
+ .to receive(:is_closed?)
+ .and_return(true)
end
it 'is not writable' do
@@ -195,6 +198,79 @@ describe WorkPackages::BaseContract do
.to match_array [:does_not_exist]
end
end
+
+ context 'transitions' do
+ let(:roles) { [FactoryBot.build_stubbed(:role)] }
+ let(:valid_transition_result) { true }
+ let(:new_status) { FactoryBot.build_stubbed(:status) }
+ let(:from_id) { work_package.status_id }
+ let(:to_id) { new_status.id }
+ let(:status_change) { work_package.status = new_status }
+
+ before do
+ new_statuses_scope = double('new statuses scope')
+
+ allow(Status)
+ .to receive(:find_by)
+ .with(id: work_package.status_id)
+ .and_return(work_package.status)
+
+ # Breaking abstraction here to avoid mocking hell.
+ # We might want to extract the assignable_... into separate
+ # objects.
+ allow(contract)
+ .to receive(:new_statuses_allowed_from)
+ .with(work_package.status)
+ .and_return(new_statuses_scope)
+
+ allow(new_statuses_scope)
+ .to receive(:exists?)
+ .with(new_status.id)
+ .and_return(valid_transition_result)
+
+ status_change
+
+ contract.validate
+ end
+
+ context 'valid transition' do
+ it 'is valid' do
+ expect(subject.errors.symbols_for(:status_id))
+ .to be_empty
+ end
+ end
+
+ context 'invalid transition' do
+ let(:valid_transition_result) { false }
+
+ it 'is invalid' do
+ expect(subject.errors.symbols_for(:status_id))
+ .to match_array [:status_transition_invalid]
+ end
+ end
+
+ context 'status is nil' do
+ let(:status_change) { work_package.status = nil }
+
+ it 'is invalid' do
+ expect(subject.errors.symbols_for(:status))
+ .to match_array [:blank]
+ end
+ end
+
+ context 'invalid transition but the type changed as well' do
+ let(:valid_transition_result) { false }
+ let(:status_change) do
+ work_package.status = new_status
+ work_package.type = FactoryBot.build_stubbed(:type)
+ end
+
+ it 'is valid' do
+ expect(subject.errors.symbols_for(:status_id))
+ .to be_empty
+ end
+ end
+ end
end
describe 'estimated hours' do
@@ -686,68 +762,207 @@ describe WorkPackages::BaseContract do
end
end
- describe 'status' do
- let(:roles) { [FactoryBot.build_stubbed(:role)] }
- let(:valid_transition_result) { true }
- let(:new_status) { FactoryBot.build_stubbed(:status) }
- let(:from_id) { work_package.status_id }
- let(:to_id) { new_status.id }
- let(:status_change) { work_package.status = new_status }
+ describe '#assignable_statuses' do
+ let(:role) { FactoryBot.build_stubbed(:role) }
+ let(:type) { FactoryBot.build_stubbed(:type) }
+ let(:assignee_user) { FactoryBot.build_stubbed(:user) }
+ let(:author_user) { FactoryBot.build_stubbed(:user) }
+ let(:current_status) { FactoryBot.build_stubbed(:status) }
+ let(:version) { FactoryBot.build_stubbed(:version) }
+ let(:work_package) do
+ FactoryBot.build_stubbed(:work_package,
+ assigned_to: assignee_user,
+ author: author_user,
+ status: current_status,
+ fixed_version: version,
+ type: type)
+ end
+ let!(:default_status) do
+ status = FactoryBot.build_stubbed(:status)
+
+ allow(Status)
+ .to receive(:default)
+ .and_return(status)
+
+ status
+ end
+
+ let(:roles) { [role] }
before do
allow(current_user)
- .to receive(:roles)
- .with(work_package.project)
- .and_return(roles)
+ .to receive(:roles_for_project)
+ .with(work_package.project)
+ .and_return(roles)
+ end
- allow(type)
- .to receive(:valid_transition?)
- .with(from_id,
- to_id,
- roles)
- .and_return(valid_transition_result)
+ shared_examples_for 'new_statuses_allowed_to' do
+ let(:base_scope) do
+ from_workflows = Workflow
+ .from_status(current_status.id, type.id, [role.id], author, assignee)
+ .select(:new_status_id)
- status_change
+ Status.where(id: from_workflows)
+ .or(Status.where(id: current_status.id))
+ end
- contract.validate
+ it 'returns a scope that returns current_status and those available by workflow' do
+ expect(contract.assignable_statuses.to_sql)
+ .to eql base_scope.order_by_position.to_sql
+ end
+
+ it 'removes closed statuses if blocked' do
+ allow(work_package)
+ .to receive(:blocked?)
+ .and_return(true)
+
+ expected = base_scope.where(is_closed: false).order_by_position
+
+ expect(contract.assignable_statuses.to_sql)
+ .to eql expected.to_sql
+ end
+
+ context 'if the current status is closed and the version is closed as well' do
+ let(:version) { FactoryBot.build_stubbed(:version, status: 'closed') }
+ let(:current_status) { FactoryBot.build_stubbed(:status, is_closed: true) }
+
+ it 'only allows the current status' do
+ expect(contract.assignable_statuses.to_sql)
+ .to eql Status.where(id: current_status.id).to_sql
+ end
+ end
end
- context 'valid transition' do
- it 'is valid' do
- expect(subject.errors.symbols_for(:status_id))
- .to be_empty
+ context 'with somebody else asking' do
+ it_behaves_like 'new_statuses_allowed_to' do
+ let(:author) { false }
+ let(:assignee) { false }
end
end
- context 'invalid transition' do
- let(:valid_transition_result) { false }
+ context 'with the author asking' do
+ let(:current_user) { author_user }
- it 'is invalid' do
- expect(subject.errors.symbols_for(:status_id))
- .to match_array [:status_transition_invalid]
+ it_behaves_like 'new_statuses_allowed_to' do
+ let(:author) { true }
+ let(:assignee) { false }
end
end
- context 'status is nil' do
- let(:status_change) { work_package.status = nil }
+ context 'with the assignee asking' do
+ let(:current_user) { assignee_user }
- it 'is invalid' do
- expect(subject.errors.symbols_for(:status))
- .to match_array [:blank]
+ it_behaves_like 'new_statuses_allowed_to' do
+ let(:author) { false }
+ let(:assignee) { true }
+ end
+ end
+
+ context 'with the assignee changing and asking as new assignee' do
+ before do
+ work_package.assigned_to = current_user
+ end
+
+ # is using the former assignee
+ it_behaves_like 'new_statuses_allowed_to' do
+ let(:author) { false }
+ let(:assignee) { false }
end
end
- context 'invalid transition but the type changed as well' do
- let(:valid_transition_result) { false }
- let(:status_change) do
+ context 'with the status having changed' do
+ let(:new_status) { FactoryBot.build_stubbed(:status) }
+
+ before do
+ allow(work_package).to receive(:persisted?).and_return(true)
+ allow(work_package).to receive(:status_id_changed?).and_return(true)
+
+ allow(Status)
+ .to receive(:find_by)
+ .with(id: work_package.status_id_was)
+ .and_return(current_status)
+
work_package.status = new_status
- work_package.type = FactoryBot.build_stubbed(:type)
end
- it 'is valid' do
- expect(subject.errors.symbols_for(:status_id))
- .to be_empty
+ it_behaves_like 'new_statuses_allowed_to' do
+ let(:author) { false }
+ let(:assignee) { false }
end
end
end
+
+ describe '#assignable_types' do
+ let(:scope) do
+ double('type scope').tap do |s|
+ allow(s)
+ .to receive(:includes)
+ .and_return(s)
+ end
+ end
+
+ context 'project nil' do
+ before do
+ work_package.project = nil
+ end
+
+ it 'is all types' do
+ allow(Type)
+ .to receive(:includes)
+ .and_return(scope)
+
+ expect(contract.assignable_types)
+ .to eql(scope)
+ end
+ end
+
+ context 'project defined' do
+ it 'is all types of the project' do
+ allow(work_package.project)
+ .to receive(:types)
+ .and_return(scope)
+
+ expect(contract.assignable_types)
+ .to eql(scope)
+ end
+ 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)
+ end
+ end
+
+ describe '#assignable_priorities' do
+ let(:active_priority) { FactoryBot.build(:priority, active: true) }
+ let(:inactive_priority) { FactoryBot.build(:priority, active: false) }
+
+ before do
+ active_priority.save!
+ inactive_priority.save!
+ 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
+ end
+ end
+
+ describe '#assignable_categories' do
+ let(:category) { double('category') }
+
+ before do
+ allow(project).to receive(:categories).and_return([category])
+ end
+
+ it 'returns all categories of the project' do
+ expect(subject.assignable_values(:category, current_user)).to match_array([category])
+ end
+ 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 b123759223a..2820c0f76d3 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
@@ -98,51 +98,6 @@ describe ::API::V3::WorkPackages::Schema::SpecificWorkPackageSchema do
end
end
- describe '#assignable_statuses_for' do
- let(:status_result) { double('status result') }
-
- before do
- allow(work_package).to receive(:persisted?).and_return(false)
- allow(work_package).to receive(:status_id_changed?).and_return(false)
- end
-
- it 'calls through to the work package' do
- expect(work_package).to receive(:new_statuses_allowed_to).with(current_user)
- .and_return(status_result)
- expect(subject.assignable_values(:status, current_user)).to eql(status_result)
- end
-
- context 'changed work package' do
- let(:work_package) do
- double('original work package',
- id: double,
- clone: cloned_wp,
- status: double('wrong status'),
- persisted?: true).as_null_object
- end
- let(:cloned_wp) do
- double('cloned work package',
- new_statuses_allowed_to: status_result)
- end
- let(:stored_status) do
- double('good status')
- end
-
- before do
- allow(work_package).to receive(:persisted?).and_return(true)
- allow(work_package).to receive(:status_id_changed?).and_return(true)
- allow(Status).to receive(:find_by)
- .with(id: work_package.status_id_was).and_return(stored_status)
- end
-
- it 'calls through to the cloned work package' do
- expect(cloned_wp).to receive(:status=).with(stored_status)
- expect(cloned_wp).to receive(:new_statuses_allowed_to).with(current_user)
- expect(subject.assignable_values(:status, current_user)).to eql(status_result)
- end
- end
- end
-
describe '#available_custom_fields' do
it 'delegates to work_package' do
expect(work_package)
diff --git a/spec/models/status_spec.rb b/spec/models/status_spec.rb
index b06fce005db..c7cf1aeedec 100644
--- a/spec/models/status_spec.rb
+++ b/spec/models/status_spec.rb
@@ -31,62 +31,6 @@ require 'spec_helper'
describe Status, type: :model do
let(:stubbed_status) { FactoryBot.build_stubbed(:status) }
- describe '.new_statuses_allowed' do
- let(:role) { FactoryBot.create(:role) }
- let(:type) { FactoryBot.create(:type) }
- let(:statuses) { (1..4).map { |_i| FactoryBot.create(:status) } }
- let(:status) { statuses[0] }
- let(:workflow_a) do
- FactoryBot.create(:workflow, role_id: role.id,
- type_id: type.id,
- old_status_id: statuses[0].id,
- new_status_id: statuses[1].id,
- author: false,
- assignee: false)
- end
- let(:workflow_b) do
- FactoryBot.create(:workflow, role_id: role.id,
- type_id: type.id,
- old_status_id: statuses[0].id,
- new_status_id: statuses[2].id,
- author: true,
- assignee: false)
- end
- let(:workflow_c) do
- FactoryBot.create(:workflow, role_id: role.id,
- type_id: type.id,
- old_status_id: statuses[0].id,
- new_status_id: statuses[3].id,
- author: false,
- assignee: true)
- end
- let(:workflows) { [workflow_a, workflow_b, workflow_c] }
-
- before do
- workflows
- end
-
- it 'should respect workflows w/o author and w/o assignee' do
- expect(Status.new_statuses_allowed(status, [role], type, false, false))
- .to match_array([statuses[1]])
- end
-
- it 'should respect workflows w/ author and w/o assignee' do
- expect(Status.new_statuses_allowed(status, [role], type, true, false))
- .to match_array([statuses[1], statuses[2]])
- end
-
- it 'should respect workflows w/o author and w/ assignee' do
- expect(Status.new_statuses_allowed(status, [role], type, false, true))
- .to match_array([statuses[1], statuses[3]])
- end
-
- it 'should respect workflows w/ author and w/ assignee' do
- expect(Status.new_statuses_allowed(status, [role], type, true, true))
- .to match_array([statuses[1], statuses[2], statuses[3]])
- end
- end
-
describe 'default status' do
context 'when default exists' do
let!(:status) { FactoryBot.create(:default_status) }
diff --git a/spec/models/work_package/work_package_relations_spec.rb b/spec/models/work_package/work_package_relations_spec.rb
index ba182636897..5a546fc7ca0 100644
--- a/spec/models/work_package/work_package_relations_spec.rb
+++ b/spec/models/work_package/work_package_relations_spec.rb
@@ -118,114 +118,6 @@ describe WorkPackage, type: :model do
end
end
- describe '#blocks' do
- let(:user) { FactoryBot.create(:user) }
- let(:role) { FactoryBot.create(:role) }
- let(:type) { FactoryBot.create(:type) }
- let(:project) do
- FactoryBot.create(:project,
- types: [type])
- end
- let(:status) { FactoryBot.create(:status) }
- let(:blocks) do
- FactoryBot.create(:work_package,
- project: project,
- status: status)
- end
- let(:blocked) do
- FactoryBot.create(:work_package,
- project: project,
- type: blocks.type,
- status: status)
- end
- let(:relation_blocks) do
- FactoryBot.create(:relation,
- from: blocks,
- to: blocked,
- relation_type: Relation::TYPE_BLOCKS)
- end
-
- before do
- relation_blocks
- end
-
- describe '#blocked?' do
- context 'blocked work package' do
- subject { blocked.blocked? }
-
- it { is_expected.to be_truthy }
- end
-
- context 'blocking work package' do
- subject { blocks.blocked? }
-
- it { is_expected.to be_falsey }
- end
- end
-
- describe 'closed state' do
- let(:project_member) do
- FactoryBot.create(:member,
- project: project,
- principal: user,
- roles: [role])
- end
- let(:workflow_1) do
- FactoryBot.create(:workflow,
- role: role,
- old_status: status,
- new_status: status)
- end
- let(:workflow_2) do
- FactoryBot.create(:workflow,
- role: role,
- old_status: status,
- new_status: closed_state)
- end
-
- shared_examples_for 'work package with status transitions' do
- subject { work_package.new_statuses_allowed_to(user) }
-
- it { is_expected.not_to be_empty }
- end
-
- shared_context 'allowed status transitions' do
- subject { work_package.new_statuses_allowed_to(user).select(&:is_closed?) }
- end
-
- before do
- project_member
-
- type.workflows << workflow_1
- type.workflows << workflow_2
- end
-
- context 'blocked work package' do
- let(:work_package) { blocked }
-
- it_behaves_like 'work package with status transitions'
-
- describe 'deny closed state' do
- include_context 'allowed status transitions'
-
- it { is_expected.to be_empty }
- end
- end
-
- context 'blocking work package' do
- let(:work_package) { blocks }
-
- it_behaves_like 'work package with status transitions'
-
- describe 'allow closed state' do
- include_context 'allowed status transitions'
-
- it { is_expected.not_to be_empty }
- end
- end
- end
- end
-
describe '#soonest_start' do
let(:work_package_1) { FactoryBot.create(:work_package) }
let(:work_package_2) do
diff --git a/spec/models/work_package/work_package_status_spec.rb b/spec/models/work_package/work_package_status_spec.rb
index 7541b9cef65..f8e6fb9424e 100644
--- a/spec/models/work_package/work_package_status_spec.rb
+++ b/spec/models/work_package/work_package_status_spec.rb
@@ -35,11 +35,6 @@ describe WorkPackage, 'status', type: :model do
status: status)
end
- it 'can read planning_elements w/ the help of the has_many association' do
- expect(WorkPackage.where(status_id: status.id).count).to eq(1)
- expect(WorkPackage.where(status_id: status.id).first).to eq(work_package)
- end
-
describe '#readonly' do
let(:status) { FactoryBot.create(:status, is_readonly: true) }
@@ -55,120 +50,4 @@ describe WorkPackage, 'status', type: :model do
end
end
end
-
- describe '#new_statuses_allowed_to' do
- let(:role) { FactoryBot.build_stubbed(:role) }
- let(:type) { FactoryBot.build_stubbed(:type) }
- let(:user) { FactoryBot.build_stubbed(:user) }
- let(:assignee_user) { FactoryBot.build_stubbed(:user) }
- let(:author_user) { FactoryBot.build_stubbed(:user) }
- let(:current_status) { FactoryBot.build_stubbed(:status) }
- let(:version) { FactoryBot.build_stubbed(:version) }
- let(:work_package) do
- FactoryBot.build_stubbed(:work_package,
- assigned_to: assignee_user,
- author: author_user,
- status: current_status,
- fixed_version: version,
- type: type)
- end
- let(:default_status) do
- status = FactoryBot.build_stubbed(:status)
-
- allow(Status)
- .to receive(:default)
- .and_return(status)
-
- status
- end
-
- let(:roles) { [role] }
-
- before do
- default_status
-
- allow(user)
- .to receive(:roles_for_project)
- .with(work_package.project)
- .and_return(roles)
- end
-
- shared_examples_for 'new_statuses_allowed_to' do
- let(:base_scope) do
- current_status
- .new_statuses_allowed_to([role], type, author, assignee)
- .or(Status.where(id: current_status.id))
- end
-
- it 'returns a scope that returns current_status and those available by workflow' do
- expect(work_package.new_statuses_allowed_to(user).to_sql)
- .to eql base_scope.order_by_position.to_sql
- end
-
- it 'adds the default status when the parameter is set accordingly' do
- expected = base_scope.or(Status.where_default).order_by_position
-
- expect(work_package.new_statuses_allowed_to(user, true).to_sql)
- .to eql expected.to_sql
- end
-
- it 'removes closed statuses if blocked' do
- allow(work_package)
- .to receive(:blocked?)
- .and_return(true)
-
- expected = base_scope.where(is_closed: false).order_by_position
-
- expect(work_package.new_statuses_allowed_to(user).to_sql)
- .to eql expected.to_sql
- end
-
- context 'if the current status is closed and the version is closed as well' do
- let(:version) { FactoryBot.build_stubbed(:version, status: 'closed') }
- let(:current_status) { FactoryBot.build_stubbed(:status, is_closed: true) }
-
- it 'only allows the current status' do
- expect(work_package.new_statuses_allowed_to(user, true).to_sql)
- .to eql Status.where(id: current_status.id).to_sql
- end
- end
- end
-
- context 'with somebody else asking' do
- it_behaves_like 'new_statuses_allowed_to' do
- let(:author) { false }
- let(:assignee) { false }
- end
- end
-
- context 'with the author asking' do
- let(:user) { author_user }
-
- it_behaves_like 'new_statuses_allowed_to' do
- let(:author) { true }
- let(:assignee) { false }
- end
- end
-
- context 'with the assignee asking' do
- let(:user) { assignee_user }
-
- it_behaves_like 'new_statuses_allowed_to' do
- let(:author) { false }
- let(:assignee) { true }
- end
- end
-
- context 'with the assignee changing and asking as new assignee' do
- before do
- work_package.assigned_to = user
- end
-
- # is using the former assignee
- it_behaves_like 'new_statuses_allowed_to' do
- let(:author) { false }
- let(:assignee) { false }
- end
- end
- end
end