Spiegel von
https://github.com/paviliondev/discourse-custom-wizard.git
synchronisiert 2024-11-22 09:20:29 +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']
|
||||
permitted_data = {}
|
||||
|
||||
permitted_params.each do |param|
|
||||
key = param['key'].to_sym
|
||||
permitted_data[key] = params[key] if params[key]
|
||||
permitted_params.each do |p|
|
||||
params_key = p['key'].to_sym
|
||||
submission_key = p['value'].to_sym
|
||||
permitted_data[submission_key] = params[params_key] if params[params_key]
|
||||
end
|
||||
|
||||
if permitted_data.present?
|
||||
|
@ -215,7 +216,7 @@ class CustomWizard::Builder
|
|||
submission = @submissions.last
|
||||
params[:value] = submission[field_template['id']] if submission[field_template['id']]
|
||||
end
|
||||
|
||||
|
||||
## If a field updates a profile field, load the current value
|
||||
if step_template['actions'] && step_template['actions'].any?
|
||||
profile_actions = step_template['actions'].select { |a| a['type'] === 'update_profile' }
|
||||
|
@ -223,7 +224,7 @@ class CustomWizard::Builder
|
|||
if profile_actions.any?
|
||||
profile_actions.each do |action|
|
||||
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
|
||||
|
|
|
@ -192,8 +192,8 @@ class CustomWizard::Wizard
|
|||
end
|
||||
end
|
||||
|
||||
def self.add_wizard(json)
|
||||
wizard = ::JSON.parse(json)
|
||||
def self.add_wizard(obj)
|
||||
wizard = obj.is_a?(String) ? ::JSON.parse(json) : obj
|
||||
PluginStore.set('custom_wizard', wizard["id"], wizard)
|
||||
end
|
||||
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
## TODO limit this to the first admin
|
||||
module SiteSerializerCWX
|
||||
attributes :complete_custom_wizard
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
attributes :complete_custom_wizard
|
||||
end
|
||||
|
||||
|
||||
def include_wizard_required?
|
||||
scope.is_admin? && Wizard.new(scope.user).requires_completion?
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
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
|
||||
return object.label if object.label.present?
|
||||
|
|
|
@ -1,14 +1,18 @@
|
|||
module CustomWizardWizardSerializerExtension
|
||||
attributes :id,
|
||||
:name,
|
||||
:background,
|
||||
:completed,
|
||||
:required,
|
||||
:min_trust,
|
||||
:permitted,
|
||||
:user,
|
||||
:categories,
|
||||
:uncategorized_category_id
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
attributes :id,
|
||||
:name,
|
||||
:background,
|
||||
:completed,
|
||||
:required,
|
||||
:min_trust,
|
||||
:permitted,
|
||||
:user,
|
||||
:categories,
|
||||
:uncategorized_category_id
|
||||
end
|
||||
|
||||
def id
|
||||
object.id
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
module CustomWizardWizardStepSerializerExtension
|
||||
attributes :permitted, :permitted_message
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
attributes :permitted, :permitted_message
|
||||
end
|
||||
|
||||
def title
|
||||
return PrettyText.cook(object.title) if object.title
|
||||
|
|
|
@ -3,18 +3,30 @@
|
|||
require 'rails_helper'
|
||||
|
||||
describe CustomWizard::Builder do
|
||||
fab!(:user) { Fabricate(:user) }
|
||||
fab!(:user) { Fabricate(:user) }
|
||||
fab!(:trusted_user) { Fabricate(:user, trust_level: 3)}
|
||||
|
||||
before do
|
||||
@template = File.open(
|
||||
let!(:template) do
|
||||
JSON.parse(File.open(
|
||||
"#{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
|
||||
|
||||
it "returns no steps when disabled" do
|
||||
CustomWizard::Wizard.add_wizard(@template)
|
||||
SiteSetting.custom_wizard_enabled = false
|
||||
wizard = CustomWizard::Builder.new(user, 'welcome').build
|
||||
wizard = build_wizard
|
||||
expect(wizard.steps.length).to eq(0)
|
||||
expect(wizard.name).to eq('Welcome')
|
||||
end
|
||||
|
@ -24,38 +36,63 @@ describe CustomWizard::Builder do
|
|||
SiteSetting.custom_wizard_enabled = true
|
||||
end
|
||||
|
||||
it "returns steps when enabled" do
|
||||
CustomWizard::Wizard.add_wizard(@template)
|
||||
wizard = CustomWizard::Builder.new(user, 'welcome').build
|
||||
expect(wizard.steps.length).to eq(2)
|
||||
it "returns steps" do
|
||||
expect(build_wizard.steps.length).to eq(2)
|
||||
end
|
||||
|
||||
it 'returns no steps if the multiple submissions are disabled and user has completed' do
|
||||
@template['multiple_submissions'] = false
|
||||
CustomWizard::Wizard.add_wizard(@template)
|
||||
wizard = CustomWizard::Builder.new(user, 'welcome').build
|
||||
expect(wizard.steps.length).to eq(0)
|
||||
it 'returns no steps if the multiple submissions are disabled and user has completed it' do
|
||||
history_params = {
|
||||
action: UserHistory.actions[:custom_wizard_step],
|
||||
acting_user_id: user.id,
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
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,
|
||||
"steps": [
|
||||
{
|
||||
"id": "step_1",
|
||||
"id": "welcome",
|
||||
"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",
|
||||
"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.",
|
||||
"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>",
|
||||
"fields": [
|
||||
{
|
||||
"id": "name",
|
||||
"type": "text",
|
||||
"label": "Name",
|
||||
"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",
|
||||
"type": "text"
|
||||
}
|
||||
|
@ -40,13 +43,12 @@
|
|||
"value": "name"
|
||||
},
|
||||
{
|
||||
"key": "field_3",
|
||||
"key": "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