Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-25 18:50:27 +01:00
Bulder tests WIP
Dieser Commit ist enthalten in:
Ursprung
ba33576f51
Commit
a96a2b965c
8 geänderte Dateien mit 111 neuen und 54 gelöschten Zeilen
|
@ -98,9 +98,10 @@ class CustomWizard::Builder
|
||||||
if permitted_params = step_template['permitted_params']
|
if permitted_params = step_template['permitted_params']
|
||||||
permitted_data = {}
|
permitted_data = {}
|
||||||
|
|
||||||
permitted_params.each do |param|
|
permitted_params.each do |p|
|
||||||
key = param['key'].to_sym
|
params_key = p['key'].to_sym
|
||||||
permitted_data[key] = params[key] if params[key]
|
submission_key = p['value'].to_sym
|
||||||
|
permitted_data[submission_key] = params[params_key] if params[params_key]
|
||||||
end
|
end
|
||||||
|
|
||||||
if permitted_data.present?
|
if permitted_data.present?
|
||||||
|
@ -215,7 +216,7 @@ class CustomWizard::Builder
|
||||||
submission = @submissions.last
|
submission = @submissions.last
|
||||||
params[:value] = submission[field_template['id']] if submission[field_template['id']]
|
params[:value] = submission[field_template['id']] if submission[field_template['id']]
|
||||||
end
|
end
|
||||||
|
|
||||||
## If a field updates a profile field, load the current value
|
## If a field updates a profile field, load the current value
|
||||||
if step_template['actions'] && step_template['actions'].any?
|
if step_template['actions'] && step_template['actions'].any?
|
||||||
profile_actions = step_template['actions'].select { |a| a['type'] === 'update_profile' }
|
profile_actions = step_template['actions'].select { |a| a['type'] === 'update_profile' }
|
||||||
|
@ -223,7 +224,7 @@ class CustomWizard::Builder
|
||||||
if profile_actions.any?
|
if profile_actions.any?
|
||||||
profile_actions.each do |action|
|
profile_actions.each do |action|
|
||||||
if update = action['profile_updates'].select { |u| u['key'] === field_template['id'] }.first
|
if update = action['profile_updates'].select { |u| u['key'] === field_template['id'] }.first
|
||||||
params[:value] = prefill_profile_field(update)
|
params[:value] = prefill_profile_field(update) || params[:value]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -192,8 +192,8 @@ class CustomWizard::Wizard
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.add_wizard(json)
|
def self.add_wizard(obj)
|
||||||
wizard = ::JSON.parse(json)
|
wizard = obj.is_a?(String) ? ::JSON.parse(json) : obj
|
||||||
PluginStore.set('custom_wizard', wizard["id"], wizard)
|
PluginStore.set('custom_wizard', wizard["id"], wizard)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,11 @@
|
||||||
## TODO limit this to the first admin
|
## TODO limit this to the first admin
|
||||||
module SiteSerializerCWX
|
module SiteSerializerCWX
|
||||||
attributes :complete_custom_wizard
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
included do
|
||||||
|
attributes :complete_custom_wizard
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
def include_wizard_required?
|
def include_wizard_required?
|
||||||
scope.is_admin? && Wizard.new(scope.user).requires_completion?
|
scope.is_admin? && Wizard.new(scope.user).requires_completion?
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
module CustomWizardWizardFieldSerializerExtension
|
module CustomWizardWizardFieldSerializerExtension
|
||||||
attributes :dropdown_none, :image, :file_types, :limit, :property
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
included do
|
||||||
|
attributes :dropdown_none, :image, :file_types, :limit, :property
|
||||||
|
end
|
||||||
|
|
||||||
def label
|
def label
|
||||||
return object.label if object.label.present?
|
return object.label if object.label.present?
|
||||||
|
|
|
@ -1,14 +1,18 @@
|
||||||
module CustomWizardWizardSerializerExtension
|
module CustomWizardWizardSerializerExtension
|
||||||
attributes :id,
|
extend ActiveSupport::Concern
|
||||||
:name,
|
|
||||||
:background,
|
included do
|
||||||
:completed,
|
attributes :id,
|
||||||
:required,
|
:name,
|
||||||
:min_trust,
|
:background,
|
||||||
:permitted,
|
:completed,
|
||||||
:user,
|
:required,
|
||||||
:categories,
|
:min_trust,
|
||||||
:uncategorized_category_id
|
:permitted,
|
||||||
|
:user,
|
||||||
|
:categories,
|
||||||
|
:uncategorized_category_id
|
||||||
|
end
|
||||||
|
|
||||||
def id
|
def id
|
||||||
object.id
|
object.id
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
module CustomWizardWizardStepSerializerExtension
|
module CustomWizardWizardStepSerializerExtension
|
||||||
attributes :permitted, :permitted_message
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
included do
|
||||||
|
attributes :permitted, :permitted_message
|
||||||
|
end
|
||||||
|
|
||||||
def title
|
def title
|
||||||
return PrettyText.cook(object.title) if object.title
|
return PrettyText.cook(object.title) if object.title
|
||||||
|
|
|
@ -3,18 +3,30 @@
|
||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
describe CustomWizard::Builder do
|
describe CustomWizard::Builder do
|
||||||
fab!(:user) { Fabricate(:user) }
|
fab!(:user) { Fabricate(:user) }
|
||||||
|
fab!(:trusted_user) { Fabricate(:user, trust_level: 3)}
|
||||||
|
|
||||||
before do
|
let!(:template) do
|
||||||
@template = File.open(
|
JSON.parse(File.open(
|
||||||
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/wizard.json"
|
"#{Rails.root}/plugins/discourse-custom-wizard/spec/fixtures/wizard.json"
|
||||||
).read
|
).read)
|
||||||
|
end
|
||||||
|
|
||||||
|
def build_wizard(t = template, u = user, build_opts = {}, params = {})
|
||||||
|
CustomWizard::Wizard.add_wizard(t)
|
||||||
|
CustomWizard::Builder.new(u, 'welcome').build(build_opts, params)
|
||||||
|
end
|
||||||
|
|
||||||
|
def add_submission_data
|
||||||
|
PluginStore.set("welcome_submissions", user.id,
|
||||||
|
name: 'Angus',
|
||||||
|
website: 'https://thepavilion.io'
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns no steps when disabled" do
|
it "returns no steps when disabled" do
|
||||||
CustomWizard::Wizard.add_wizard(@template)
|
|
||||||
SiteSetting.custom_wizard_enabled = false
|
SiteSetting.custom_wizard_enabled = false
|
||||||
wizard = CustomWizard::Builder.new(user, 'welcome').build
|
wizard = build_wizard
|
||||||
expect(wizard.steps.length).to eq(0)
|
expect(wizard.steps.length).to eq(0)
|
||||||
expect(wizard.name).to eq('Welcome')
|
expect(wizard.name).to eq('Welcome')
|
||||||
end
|
end
|
||||||
|
@ -24,38 +36,63 @@ describe CustomWizard::Builder do
|
||||||
SiteSetting.custom_wizard_enabled = true
|
SiteSetting.custom_wizard_enabled = true
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns steps when enabled" do
|
it "returns steps" do
|
||||||
CustomWizard::Wizard.add_wizard(@template)
|
expect(build_wizard.steps.length).to eq(2)
|
||||||
wizard = CustomWizard::Builder.new(user, 'welcome').build
|
|
||||||
expect(wizard.steps.length).to eq(2)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns no steps if the multiple submissions are disabled and user has completed' do
|
it 'returns no steps if the multiple submissions are disabled and user has completed it' do
|
||||||
@template['multiple_submissions'] = false
|
history_params = {
|
||||||
CustomWizard::Wizard.add_wizard(@template)
|
action: UserHistory.actions[:custom_wizard_step],
|
||||||
wizard = CustomWizard::Builder.new(user, 'welcome').build
|
acting_user_id: user.id,
|
||||||
expect(wizard.steps.length).to eq(0)
|
context: template['id']
|
||||||
|
}
|
||||||
|
UserHistory.create!(history_params.merge(subject: template['steps'][0]['id']))
|
||||||
|
UserHistory.create!(history_params.merge(subject: template['steps'][1]['id']))
|
||||||
|
|
||||||
|
template["multiple_submissions"] = false
|
||||||
|
expect(build_wizard(template).steps.length).to eq(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns nothing if the user is not permitted to see it' do
|
it 'returns no steps if has min trust and user does not meet it' do
|
||||||
|
template["min_trust"] = 3
|
||||||
|
expect(build_wizard(template).steps.length).to eq(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns a wizard with prefilled data if user has partially completed' do
|
it 'returns steps if it has min trust and user meets it' do
|
||||||
|
template["min_trust"] = 3
|
||||||
|
expect(build_wizard(template, trusted_user).steps.length).to eq(2)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns a wizard with prefilled data if user has partially completed it' do
|
||||||
|
add_submission_data
|
||||||
|
wizard = build_wizard
|
||||||
|
expect(wizard.steps[0].fields.first.value).to eq('Angus')
|
||||||
|
expect(wizard.steps[1].fields.first.value).to eq('https://thepavilion.io')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns a wizard with no prefilled data if options include reset' do
|
it 'returns a wizard with no prefilled data if options include reset' do
|
||||||
|
add_submission_data
|
||||||
|
wizard = build_wizard(template, user, reset: true)
|
||||||
|
expect(wizard.steps[0].fields.first.value).to eq(nil)
|
||||||
|
expect(wizard.steps[1].fields.first.value).to eq(nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'building steps' do
|
context 'building steps' do
|
||||||
it 'returns step data correctly' do
|
it 'returns step metadata' do
|
||||||
|
expect(build_wizard.steps[0].title).to eq('Welcome to Pavilion')
|
||||||
|
expect(build_wizard.steps[1].title).to eq('Tell us about you')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'saves permitted params' do
|
it 'saves permitted params' do
|
||||||
|
template['steps'][0]['permitted_params'] = [
|
||||||
|
{
|
||||||
|
"key": "param_key",
|
||||||
|
"value": "submission_param_key"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
wizard = build_wizard(template, user, {}, param_key: 'param_value')
|
||||||
|
submissions = PluginStore.get("welcome_submissions", user.id)
|
||||||
|
expect(submissions.first['submission_param_key']).to eq('param_value')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'ensures required data is present' do
|
it 'ensures required data is present' do
|
||||||
|
|
26
spec/fixtures/wizard.json
gevendort
26
spec/fixtures/wizard.json
gevendort
|
@ -8,24 +8,27 @@
|
||||||
"theme_id": 4,
|
"theme_id": 4,
|
||||||
"steps": [
|
"steps": [
|
||||||
{
|
{
|
||||||
"id": "step_1",
|
"id": "welcome",
|
||||||
"title": "Welcome to Pavilion",
|
"title": "Welcome to Pavilion",
|
||||||
"raw_description": "Hey there, thanks for signing up.\n\nWe're Pavilion, an international freelancer cooperative that specialises in online communities.\n\nThis site is our own community, where we work with our clients, users of our open source work and our broader community.\n\n",
|
"raw_description": "Hey there, thanks for signing up.\n\nWe're Pavilion, an international freelancer cooperative that specialises in online communities.\n\nThis site is our own community, where we work with our clients, users of our open source work and our broader community.\n\n",
|
||||||
"description": "<p>Hey there, thanks for signing up.</p>\n<p>We’re Pavilion, an international freelancer cooperative that specialises in online communities.</p>\n<p>This site is our own community, where we work with our clients, users of our open source work and our broader community.</p>"
|
"description": "<p>Hey there, thanks for signing up.</p>\n<p>We’re Pavilion, an international freelancer cooperative that specialises in online communities.</p>\n<p>This site is our own community, where we work with our clients, users of our open source work and our broader community.</p>",
|
||||||
},
|
|
||||||
{
|
|
||||||
"id": "step_2",
|
|
||||||
"title": "Tell us about you",
|
|
||||||
"raw_description": "We'd like to know a little more about you. Add a your name and your website below. This will update your user profile.",
|
|
||||||
"fields": [
|
"fields": [
|
||||||
{
|
{
|
||||||
"id": "name",
|
"id": "name",
|
||||||
"type": "text",
|
"type": "text",
|
||||||
"label": "Name",
|
"label": "Name",
|
||||||
"required": true
|
"required": true
|
||||||
},
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "about_you",
|
||||||
|
"title": "Tell us about you",
|
||||||
|
"raw_description": "We'd like to know a little more about you. Add a your name and your website below. This will update your user profile.",
|
||||||
|
"description": "<p>We’d like to know a little more about you. Add a your name and your website below. This will update your user profile.</p>",
|
||||||
|
"fields": [
|
||||||
{
|
{
|
||||||
"id": "field_3",
|
"id": "website",
|
||||||
"label": "Website",
|
"label": "Website",
|
||||||
"type": "text"
|
"type": "text"
|
||||||
}
|
}
|
||||||
|
@ -40,13 +43,12 @@
|
||||||
"value": "name"
|
"value": "name"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"key": "field_3",
|
"key": "website",
|
||||||
"value": "website"
|
"value": "website"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
]
|
||||||
"description": "<p>We’d like to know a little more about you. Add a your name and your website below. This will update your user profile.</p>"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
Laden …
In neuem Issue referenzieren