2018-02-17 00:10:30 -05:00
|
|
|
# encoding: UTF-8
|
2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
2018-02-17 00:10:30 -05:00
|
|
|
|
|
|
|
describe MaxEmojisValidator do
|
|
|
|
|
|
|
|
# simulate Rails behavior (singleton)
|
|
|
|
def validate
|
|
|
|
@validator ||= MaxEmojisValidator.new(attributes: :title)
|
|
|
|
@validator.validate_each(record, :title, record.title)
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "validating any topic title" do
|
|
|
|
it 'adds an error when emoji count is greater than SiteSetting.max_emojis_in_title' do
|
|
|
|
SiteSetting.max_emojis_in_title = 3
|
2019-03-06 06:49:17 -05:00
|
|
|
CustomEmoji.create!(name: 'trout', upload: Fabricate(:upload))
|
|
|
|
Emoji.clear_cache
|
|
|
|
record.title = '🧐 Lots of emojis here 🎃 :trout: :)'
|
2018-02-17 00:10:30 -05:00
|
|
|
validate
|
2018-02-21 01:02:40 -05:00
|
|
|
expect(record.errors[:title][0]).to eq(I18n.t("errors.messages.max_emojis", max_emojis_count: 3))
|
2018-03-05 09:35:24 -05:00
|
|
|
|
2021-05-20 21:43:47 -04:00
|
|
|
record.title = ':joy: :blush: :smile: is not only about emojis: Happiness::start()'
|
2018-03-05 09:35:24 -05:00
|
|
|
validate
|
|
|
|
expect(record.valid?).to be true
|
2018-02-17 00:10:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'topic' do
|
|
|
|
let(:record) { Fabricate.build(:topic) }
|
|
|
|
|
|
|
|
it 'does not add an error when emoji count is good' do
|
|
|
|
SiteSetting.max_emojis_in_title = 2
|
|
|
|
|
|
|
|
record.title = 'To Infinity and beyond! 🚀 :woman:t5:'
|
|
|
|
validate
|
|
|
|
expect(record.errors[:title]).to_not be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "validating any topic title"
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'private message' do
|
|
|
|
let(:record) { Fabricate.build(:private_message_topic) }
|
|
|
|
|
|
|
|
it 'does not add an error when emoji count is good' do
|
|
|
|
SiteSetting.max_emojis_in_title = 1
|
|
|
|
|
|
|
|
record.title = 'To Infinity and beyond! 🚀'
|
|
|
|
validate
|
|
|
|
expect(record.errors[:title]).to_not be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples "validating any topic title"
|
|
|
|
end
|
|
|
|
end
|