discourse/plugins/discourse-policy/spec/plugin_spec.rb
Sérgio Saquetim 726fc21187
DEV: Enforce deprecation-free tests for preinstalled plugins/themes (#36445)
This PR implements stricter deprecation handling that enforces
deprecation-free tests for core and preinstalled plugins, while allowing
custom (non-preinstalled) plugins and themes to have deprecations
without causing test failures.

### Key Changes

#### CI Workflow Improvements
- Split plugin system tests into separate CI targets: `core-plugins`,
`official-plugins`, and `chat`
- Enhance `bin/turbo_rspec` to accept comma-separated exclude patterns
via `--exclude-pattern`
- Simplify workflow configuration with default `shell: bash` and
consolidated environment variables

#### Plugin Classification & Detection
- Centralize official plugins list in `config/official_plugins.json` for
unified backend and frontend access
- Detect preinstalled plugins by checking for absence of `.git`
directory
- Add `isOfficial` and `isPreinstalled` metadata flags to plugin info
- Add `data-preinstalled` and `data-official` attributes to all plugin
and theme script tags for runtime identification

#### Deprecation Source Tracking
- Track deprecation sources (core, plugin, or theme) through template
map and resolver to attribute deprecations correctly
- Improve `source-identifier.js` to detect admin UI plugin files in both
development and production environments
- Add source information to deprecation messages for better debugging

#### Test Infrastructure
- Modify `raise-on-deprecation` test helper to skip errors for custom
(non-preinstalled) plugins and themes
- Add `EMBER_RAISE_ON_DEPRECATION` environment variable to control
deprecation throwing behavior in Rails tests
- Automatically set `EMBER_RAISE_ON_DEPRECATION` for core and
preinstalled plugin/theme specs in `rails_helper.rb`
- Improve deprecation summary output for system specs with test/spec
origin tracking

#### Deprecation Workflow Enhancements
- Add `dont-throw` handler for selective deprecation bypassing in test
fixtures without raising errors
- Add `dont-count` handler for preventing deprecation counting in
specific scenarios (e.g., test fixtures)

#### Deprecation Fixes
- Fix pending deprecations across core plugins (chat, data-explorer,
discourse-subscriptions, gamification, house-ads, reactions,
rss-polling, styleguide)
- Update import paths and remove deprecated patterns
- Migrate deprecated Handlebars templates to JavaScript API

### Testing Strategy

With these changes:
- **Core and preinstalled plugins** must pass all tests without any
deprecations
- **Custom plugins and themes** can have deprecations without failing
tests
- Test fixtures can use `dont-throw` and `dont-count` handlers when
testing deprecation behavior itself
- System specs automatically configure deprecation enforcement based on
test file location

---------

Co-authored-by: David Taylor <david@taylorhq.com>
Co-authored-by: Jarek Radosz <jradosz@gmail.com>
2025-12-16 17:48:29 -03:00

124 lines
3.6 KiB
Ruby

# frozen_string_literal: true
describe DiscoursePolicy do
fab!(:user1, :user)
before { enable_current_plugin }
describe "after_initialize" do
before { Jobs.run_immediately! }
it "serializes user options correctly" do
user1.user_option.update(
policy_email_frequency: UserOption.policy_email_frequencies[:when_away],
)
@plugin = Plugin::Instance.new
@plugin.add_to_serializer(:user_option, :policy_email_frequency) do
object.policy_email_frequency
end
json = UserSerializer.new(user1, scope: Guardian.new(user1), root: false).as_json
expect(json[:user_option][:policy_email_frequency]).to eq("when_away")
end
end
describe "post_process_cooked event" do
before { Jobs.run_immediately! }
fab!(:group)
fab!(:moderator)
it "sets next_renew_at when removing renew-start but not renew" do
renew_days = 10
renew_start = 1.day.from_now.to_date
raw = <<~MD
[policy group=#{group.name} renew="#{renew_days}" renew-start="#{renew_start.strftime("%F")}"]
Here's the new policy
[/policy]
MD
post = create_post(raw: raw, user: Fabricate(:admin))
policy = PostPolicy.find_by(post: post)
expect(policy.renew_days).to eq(renew_days)
expect(policy.renew_start).to eq(renew_start)
expect(policy.next_renew_at.to_date).to eq(renew_start)
updated_policy = <<~MD
[policy group=#{group.name} renew="#{renew_days}"]
Here's the new policy
[/policy]
MD
post.update!(raw: updated_policy)
post.rebake!
policy = policy.reload
expect(policy.renew_days).to eq(renew_days)
expect(policy.renew_start).to be_nil
expect(policy.next_renew_at).to be_nil
end
context "with add_users_to_group present" do
fab!(:group2, :group)
fab!(:post) { Fabricate(:post, user: moderator) }
fab!(:policy666) do
policy = Fabricate(:post_policy, post: post, add_users_to_group: group2.id)
PostPolicyGroup.create!(post_policy_id: policy.id, group_id: group.id)
policy
end
before { group.add(user1) }
it "removes all users from the group upon version change" do
updated_policy = <<~MD
[policy group=#{group.name} version=2 add_users_to_group=#{group2.id}]
Here's the new policy
[/policy]
MD
post.update!(raw: updated_policy)
post.rebake!
post.post_policy.reload
expect(group2.users).to contain_exactly
end
end
end
describe "current user serializer extensions" do
let(:serializer) { CurrentUserSerializer.new(user1, scope: Guardian.new(user1)) }
fab!(:group)
before { SiteSetting.create_policy_allowed_groups = "1|2|#{group.id}" }
it "returns false when user is not in a policy creation group" do
expect(serializer.can_create_policy).to be_falsey
end
it "returns true when user is in a policy creation group" do
group.add(user1)
expect(serializer.can_create_policy).to be_truthy
end
end
describe "deprecated settings" do
let(:fake_logger) { FakeLogger.new }
before { Rails.logger.broadcast_to(fake_logger) }
after { Rails.logger.stop_broadcasting_to(fake_logger) }
it "logs deprecation warning" do
SiteSetting.policy_restrict_to_staff_posts
expect(fake_logger.warnings[0]).to include(
"DEPRECATION NOTICE: `SiteSetting.policy_restrict_to_staff_posts` has been deprecated. Please use `SiteSetting.create_policy_allowed_groups` instead. (removal in Discourse 3.7.0)",
)
end
end
end