-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #269 from paviliondev/composer-control-feature
Composer control feature
- Loading branch information
Showing
5 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# frozen_string_literal: true | ||
# name: discourse-custom-wizard | ||
# about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more. | ||
# version: 2.4.22 | ||
# version: 2.4.23 | ||
# authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever, Juan Marcos Gutierrez Ramos | ||
# url: https://github.com/paviliondev/discourse-custom-wizard | ||
# contact_emails: [email protected] | ||
|
@@ -236,4 +236,13 @@ | |
end | ||
|
||
DiscourseEvent.trigger(:custom_wizard_ready) | ||
|
||
on(:before_create_topic) do |topic_params, user| | ||
category = topic_params.category | ||
if category&.custom_fields&.[]('create_topic_wizard').present? | ||
raise Discourse::InvalidParameters.new( | ||
I18n.t('wizard.error_messages.wizard_replacing_composer') | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Topic, type: :model do | ||
fab!(:category_with_wizard) do | ||
Fabricate(:category, custom_fields: { create_topic_wizard: 'true' }) | ||
end | ||
fab!(:category_without_wizard) { Fabricate(:category) } | ||
fab!(:user) { Fabricate(:user) } | ||
let(:valid_attrs) { Fabricate.attributes_for(:topic) } | ||
|
||
context 'with a create_topic_wizard custom field in the category' do | ||
it 'will not allow creating a topic directly' do | ||
expect do | ||
TopicCreator.create( | ||
user, | ||
Guardian.new(user), | ||
valid_attrs.merge( | ||
title: 'A valid and sufficiently long title for testing', | ||
category: category_with_wizard.id, | ||
raw: 'hello this is a test topic with category with custom fields' | ||
) | ||
) | ||
end.to raise_error( | ||
Discourse::InvalidParameters, | ||
'Category not allowed for topic creation.' | ||
) | ||
end | ||
end | ||
|
||
context 'without a create_topic_wizard custom field in the category' do | ||
it 'will allow creating a topic directly' do | ||
expect do | ||
TopicCreator.create( | ||
user, | ||
Guardian.new(user), | ||
valid_attrs.merge( | ||
category: category_without_wizard.id, | ||
title: 'Another valid and sufficiently long title for testing', | ||
raw: 'This is the body of a valid topic' | ||
) | ||
) | ||
end.not_to raise_error | ||
end | ||
end | ||
end |
82 changes: 82 additions & 0 deletions
82
test/javascripts/acceptance/category-chooser-initializer-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { click, visit } from "@ember/test-helpers"; | ||
import { acceptance } from "discourse/tests/helpers/qunit-helpers"; | ||
import selectKit from "discourse/tests/helpers/select-kit-helper"; | ||
import { test } from "qunit"; | ||
|
||
acceptance("Category Chooser Initializer", function (needs) { | ||
needs.user(); | ||
needs.settings({ | ||
allow_uncategorized_topics: false, | ||
}); | ||
needs.site({ | ||
can_tag_topics: true, | ||
categories: [ | ||
{ | ||
id: 1, | ||
name: "General", | ||
slug: "general", | ||
permission: 1, | ||
topic_template: null, | ||
}, | ||
{ | ||
id: 2, | ||
name: "Category with custom field", | ||
slug: "category-custom-field", | ||
permission: 1, | ||
topic_template: "", | ||
custom_fields: { | ||
create_topic_wizard: "21", | ||
}, | ||
}, | ||
{ | ||
id: 3, | ||
name: "Category 1", | ||
slug: "category-1", | ||
permission: 1, | ||
topic_template: "", | ||
}, | ||
{ | ||
id: 4, | ||
name: "Category 2", | ||
slug: "category-2", | ||
permission: 1, | ||
topic_template: "", | ||
}, | ||
], | ||
}); | ||
|
||
test("does not display category with create_topic_wizard custom field", async function (assert) { | ||
const categoryChooser = selectKit(".category-chooser"); | ||
|
||
await visit("/"); | ||
await click("#create-topic"); | ||
await categoryChooser.expand(); | ||
let categories = Array.from( | ||
document.querySelectorAll(".category-chooser .category-row") | ||
).filter((category) => category.getAttribute("data-name")); // Filter elements with a data-name attribute | ||
assert.equal( | ||
categories.length, | ||
3, | ||
"Correct number of categories are displayed" | ||
); | ||
const categoryNames = ["General", "Category 1", "Category 2"]; | ||
|
||
categoryNames.forEach((categoryName) => { | ||
assert.ok( | ||
categories.some( | ||
(category) => category.getAttribute("data-name") === categoryName | ||
), | ||
`Category '${categoryName}' is displayed` | ||
); | ||
}); | ||
|
||
const categoryNameWithCustomField = "Category with custom field"; | ||
assert.notOk( | ||
categories.some( | ||
(category) => | ||
category.getAttribute("data-name") === categoryNameWithCustomField | ||
), | ||
`Category '${categoryNameWithCustomField}' is not displayed` | ||
); | ||
}); | ||
}); |