Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WorkflowActionForm: run service as a background job #6435

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions app/forms/hyrax/forms/workflow_action_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ def initialize(current_ability:, work:, attributes: {})

def save
return false unless valid?
Workflow::WorkflowActionService.run(subject: subject,
action: sipity_workflow_action,
comment: comment)

WorkflowActionJob.perform_later(
comment: comment,
name: name,
user: current_user,
work_id: work.id.to_s,
workflow: subject.entity.workflow
)

true
end

Expand Down
14 changes: 14 additions & 0 deletions app/jobs/workflow_action_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true
class WorkflowActionJob < Hyrax::ApplicationJob
def perform(comment: nil, name: false, user:, work_id:, workflow:)
work = Hyrax.query_service.find_by(id: work_id)
subject = ::Hyrax::WorkflowActionInfo.new(work, user)
action = Sipity::WorkflowAction(name, workflow)

::Hyrax::Workflow::WorkflowActionService.run(
subject: subject,
action: action,
comment: comment
)
end
end
59 changes: 0 additions & 59 deletions spec/features/workflow_state_changes_spec.rb

This file was deleted.

84 changes: 14 additions & 70 deletions spec/forms/hyrax/forms/workflow_action_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,15 @@
it 'gives false for failure' do
expect(form.save).to be false
end

it 'will not add a comment' do
expect { form.save }.not_to change { Sipity::Comment.count }
end

it 'will not send the #deliver_on_action_taken message to Hyrax::Workflow::NotificationService' do
expect(Hyrax::Workflow::NotificationService)
.not_to receive(:deliver_on_action_taken)

form.save
end

it 'will not send the #handle_action_taken message to Hyrax::Workflow::ActionTakenService' do
expect(Hyrax::Workflow::ActionTakenService)
.not_to receive(:handle_action_taken)

form.save
end
end
end

context 'if the given user can perform the given action' do
before do
allow(described_class).to receive(:workflow_action_for).with('an_action', scope: sipity_entity.workflow).and_return(an_action)
allow(described_class)
.to receive(:workflow_action_for)
.with('an_action', scope: sipity_entity.workflow)
.and_return(an_action)

expect(Hyrax::Workflow::PermissionQuery)
.to receive(:authorized_for_processing?)
Expand All @@ -86,57 +71,16 @@
expect(form.save).to be true
end

context 'and the action has a resulting_workflow_state_id' do
it 'will update the state of the given work and index it' do
expect(work).to receive(:update_index)

expect { form.save }
.to change { sipity_entity.reload.workflow_state_id }
.from(2)
.to(an_action.resulting_workflow_state_id)
end
end

context 'and the action does not have a resulting_workflow_state_id' do
let(:an_action) do
instance_double(Sipity::WorkflowAction,
resulting_workflow_state_id: nil,
notifiable_contexts: [],
triggered_methods: Sipity::Method.none)
end

it 'will not update the state of the given work' do
expect { form.save }
.not_to change { sipity_entity.reload.workflow_state_id }
end
end

it 'will create the given comment for the entity' do
expect { form.save }
.to change { Sipity::Comment.count }
.by(1)
end

it 'will send the #deliver_on_action_taken message to Hyrax::Workflow::NotificationService' do
expect(Hyrax::Workflow::NotificationService)
.to receive(:deliver_on_action_taken)
.with(entity: sipity_entity,
comment: kind_of(Sipity::Comment),
action: an_action,
user: user)

form.save
end

it 'will send the #handle_action_taken message to Hyrax::Workflow::ActionTakenService' do
expect(Hyrax::Workflow::ActionTakenService)
.to receive(:handle_action_taken)
.with(target: work,
comment: kind_of(Sipity::Comment),
action: an_action,
user: user)

form.save
it 'enqueues WorkflowActionJob' do
expect(WorkflowActionJob).to(
receive(:perform_later).with(
comment: 'a_comment',
name: 'an_action',
user: user,
work_id: work.id.to_s,
workflow: sipity_entity.workflow
)
)
end
end
end
Expand Down
77 changes: 77 additions & 0 deletions spec/services/hyrax/workflow/workflow_action_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# frozen_string_literal: true
require "spec_helper"

RSpec.describe Hyrax::Workflow::WorkflowActionService, :clean_repo do
let(:user) { FactoryBot.create(:user) }
let(:work) { FactoryBot.valkyrie_create(:hyrax_work) }

let(:sipity_entity) do
FactoryBot
.create(:sipity_entity,
proxy_for_global_id: work.to_global_id.to_s,
workflow_state_id: 2)
end

let(:an_action) do
instance_double(Sipity::WorkflowAction,
resulting_workflow_state_id: 3,
notifiable_contexts: [],
triggered_methods: Sipity::Method.none)
end

describe '.run' do
subject do
described_class.run(
subject: ::Hyrax::WorkflowActionInfo.new(work, user),
action: an_action,
comment: 'a_comment'
)
end

context 'the action has a resulting_workflow_state_id' do
it 'will update the state of the given work and index it' do
expect(work).to receive(:update_index)

expect(sipity_entity.reload.workflow_state_id)
.to change
.from(2)
.to(an_action.resulting_workflow_state_id)
end
end

context 'and the action does not have a resulting_workflow_state_id' do
let(:an_action) do
instance_double(Sipity::WorkflowAction,
resulting_workflow_state_id: nil,
notifiable_contexts: [],
triggered_methods: Sipity::Method.none)
end

it 'will not update the state of the given work' do
expect(sipity_entity.reload.workflow_state_id).not_to change
end
end

it 'will create the given comment for the entity' do
expect(Sipity::Comment.count).to change.by(1)
end

it 'will send the #deliver_on_action_taken message to Hyrax::Workflow::NotificationService' do
expect(Hyrax::Workflow::NotificationService)
.to receive(:deliver_on_action_taken)
.with(entity: sipity_entity,
comment: kind_of(Sipity::Comment),
action: an_action,
user: user)
end

it 'will send the #handle_action_taken message to Hyrax::Workflow::ActionTakenService' do
expect(Hyrax::Workflow::ActionTakenService)
.to receive(:handle_action_taken)
.with(target: work,
comment: kind_of(Sipity::Comment),
action: an_action,
user: user)
end
end
end