diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 21:42:06 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-08-20 21:42:06 +0300 |
commit | 6e4e1050d9dba2b7b2523fdd1768823ab85feef4 (patch) | |
tree | 78be5963ec075d80116a932011d695dd33910b4e /spec/lib/gitlab/i18n | |
parent | 1ce776de4ae122aba3f349c02c17cebeaa8ecf07 (diff) |
Add latest changes from gitlab-org/gitlab@13-3-stable-ee
Diffstat (limited to 'spec/lib/gitlab/i18n')
-rw-r--r-- | spec/lib/gitlab/i18n/po_linter_spec.rb | 74 | ||||
-rw-r--r-- | spec/lib/gitlab/i18n/translation_entry_spec.rb | 166 |
2 files changed, 219 insertions, 21 deletions
diff --git a/spec/lib/gitlab/i18n/po_linter_spec.rb b/spec/lib/gitlab/i18n/po_linter_spec.rb index 5dfc9d3613c..cfa39d95ebd 100644 --- a/spec/lib/gitlab/i18n/po_linter_spec.rb +++ b/spec/lib/gitlab/i18n/po_linter_spec.rb @@ -6,7 +6,7 @@ require 'simple_po_parser' # Disabling this cop to allow for multi-language examples in comments # rubocop:disable Style/AsciiComments RSpec.describe Gitlab::I18n::PoLinter do - let(:linter) { described_class.new(po_path) } + let(:linter) { described_class.new(po_path: po_path, html_todolist: {}) } let(:po_path) { 'spec/fixtures/valid.po' } def fake_translation(msgid:, translation:, plural_id: nil, plurals: []) @@ -23,8 +23,9 @@ RSpec.describe Gitlab::I18n::PoLinter do end Gitlab::I18n::TranslationEntry.new( - data, - plurals.size + 1 + entry_data: data, + nplurals: plurals.size + 1, + html_allowed: nil ) end @@ -145,6 +146,67 @@ RSpec.describe Gitlab::I18n::PoLinter do expect(errors[message_id]).to include(expected_error) end end + + context 'when an entry contains html' do + let(:po_path) { 'spec/fixtures/potential_html.po' } + + it 'presents an error for each component containing angle brackets' do + message_id = 'String with some <strong>emphasis</strong>' + + expect(errors[message_id]).to match_array [ + a_string_starting_with('contains < or >.'), + a_string_starting_with('plural id contains < or >.'), + a_string_starting_with('translation contains < or >.') + ] + end + end + + context 'when an entry contains html on the todolist' do + subject(:linter) { described_class.new(po_path: po_path, html_todolist: todolist) } + + let(:po_path) { 'spec/fixtures/potential_html.po' } + let(:todolist) do + { + 'String with a legitimate < use' => { + 'plural_id' => 'String with lots of < > uses', + 'translations' => [ + 'Translated string with a legitimate < use', + 'Translated string with lots of < > uses' + ] + } + } + end + + it 'does not present an error' do + message_id = 'String with a legitimate < use' + + expect(errors[message_id]).to be_nil + end + end + + context 'when an entry on the html todolist has changed' do + subject(:linter) { described_class.new(po_path: po_path, html_todolist: todolist) } + + let(:po_path) { 'spec/fixtures/potential_html.po' } + let(:todolist) do + { + 'String with a legitimate < use' => { + 'plural_id' => 'String with lots of < > uses', + 'translations' => [ + 'Translated string with a different legitimate < use', + 'Translated string with lots of < > uses' + ] + } + } + end + + it 'presents an error for the changed component' do + message_id = 'String with a legitimate < use' + + expect(errors[message_id]) + .to include a_string_starting_with('translation contains < or >.') + end + end end describe '#parse_po' do @@ -200,6 +262,7 @@ RSpec.describe Gitlab::I18n::PoLinter do expect(linter).to receive(:validate_number_of_plurals).with([], fake_entry) expect(linter).to receive(:validate_unescaped_chars).with([], fake_entry) expect(linter).to receive(:validate_translation).with([], fake_entry) + expect(linter).to receive(:validate_html).with([], fake_entry) linter.validate_entry(fake_entry) end @@ -212,8 +275,9 @@ RSpec.describe Gitlab::I18n::PoLinter do allow(linter).to receive(:metadata_entry).and_return(fake_metadata) fake_entry = Gitlab::I18n::TranslationEntry.new( - { msgid: 'the singular', msgid_plural: 'the plural', 'msgstr[0]' => 'the singular' }, - 2 + entry_data: { msgid: 'the singular', msgid_plural: 'the plural', 'msgstr[0]' => 'the singular' }, + nplurals: 2, + html_allowed: nil ) errors = [] diff --git a/spec/lib/gitlab/i18n/translation_entry_spec.rb b/spec/lib/gitlab/i18n/translation_entry_spec.rb index 76879f75bec..2c95b0b0124 100644 --- a/spec/lib/gitlab/i18n/translation_entry_spec.rb +++ b/spec/lib/gitlab/i18n/translation_entry_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do describe '#singular_translation' do it 'returns the normal `msgstr` for translations without plural' do data = { msgid: 'Hello world', msgstr: 'Bonjour monde' } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry.singular_translation).to eq('Bonjour monde') end @@ -18,7 +18,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do 'msgstr[0]' => 'Bonjour monde', 'msgstr[1]' => 'Bonjour mondes' } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry.singular_translation).to eq('Bonjour monde') end @@ -27,7 +27,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do describe '#all_translations' do it 'returns all translations for singular translations' do data = { msgid: 'Hello world', msgstr: 'Bonjour monde' } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry.all_translations).to eq(['Bonjour monde']) end @@ -39,7 +39,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do 'msgstr[0]' => 'Bonjour monde', 'msgstr[1]' => 'Bonjour mondes' } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry.all_translations).to eq(['Bonjour monde', 'Bonjour mondes']) end @@ -52,7 +52,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do msgid_plural: 'Hello worlds', 'msgstr[0]' => 'Bonjour monde' } - entry = described_class.new(data, 1) + entry = described_class.new(entry_data: data, nplurals: 1, html_allowed: nil) expect(entry.plural_translations).to eq(['Bonjour monde']) end @@ -65,7 +65,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do 'msgstr[1]' => 'Bonjour mondes', 'msgstr[2]' => 'Bonjour tous les mondes' } - entry = described_class.new(data, 3) + entry = described_class.new(entry_data: data, nplurals: 3, html_allowed: nil) expect(entry.plural_translations).to eq(['Bonjour mondes', 'Bonjour tous les mondes']) end @@ -77,7 +77,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do msgid: 'hello world', msgstr: 'hello' } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry).to have_singular_translation end @@ -89,7 +89,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do "msgstr[0]" => 'hello world', "msgstr[1]" => 'hello worlds' } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry).to have_singular_translation end @@ -100,7 +100,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do msgid_plural: 'hello worlds', "msgstr[0]" => 'hello worlds' } - entry = described_class.new(data, 1) + entry = described_class.new(entry_data: data, nplurals: 1, html_allowed: nil) expect(entry).not_to have_singular_translation end @@ -109,7 +109,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do describe '#msgid_contains_newlines' do it 'is true when the msgid is an array' do data = { msgid: %w(hello world) } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry.msgid_has_multiple_lines?).to be_truthy end @@ -118,7 +118,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do describe '#plural_id_contains_newlines' do it 'is true when the msgid is an array' do data = { msgid_plural: %w(hello world) } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry.plural_id_has_multiple_lines?).to be_truthy end @@ -127,7 +127,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do describe '#translations_contain_newlines' do it 'is true when the msgid is an array' do data = { msgstr: %w(hello world) } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry.translations_have_multiple_lines?).to be_truthy end @@ -135,7 +135,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do describe '#contains_unescaped_chars' do let(:data) { { msgid: '' } } - let(:entry) { described_class.new(data, 2) } + let(:entry) { described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) } it 'is true when the msgid is an array' do string = '「100%確定」' @@ -177,7 +177,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do describe '#msgid_contains_unescaped_chars' do it 'is true when the msgid contains a `%`' do data = { msgid: '「100%確定」' } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry).to receive(:contains_unescaped_chars?).and_call_original expect(entry.msgid_contains_unescaped_chars?).to be_truthy @@ -187,7 +187,7 @@ RSpec.describe Gitlab::I18n::TranslationEntry do describe '#plural_id_contains_unescaped_chars' do it 'is true when the plural msgid contains a `%`' do data = { msgid_plural: '「100%確定」' } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry).to receive(:contains_unescaped_chars?).and_call_original expect(entry.plural_id_contains_unescaped_chars?).to be_truthy @@ -197,10 +197,144 @@ RSpec.describe Gitlab::I18n::TranslationEntry do describe '#translations_contain_unescaped_chars' do it 'is true when the translation contains a `%`' do data = { msgstr: '「100%確定」' } - entry = described_class.new(data, 2) + entry = described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) expect(entry).to receive(:contains_unescaped_chars?).and_call_original expect(entry.translations_contain_unescaped_chars?).to be_truthy end end + + describe '#msgid_contains_potential_html?' do + subject(:entry) { described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) } + + context 'when there are no angle brackets in the msgid' do + let(:data) { { msgid: 'String with no brackets' } } + + it 'returns false' do + expect(entry.msgid_contains_potential_html?).to be_falsey + end + end + + context 'when there are angle brackets in the msgid' do + let(:data) { { msgid: 'String with <strong> tag' } } + + it 'returns true' do + expect(entry.msgid_contains_potential_html?).to be_truthy + end + end + end + + describe '#plural_id_contains_potential_html?' do + subject(:entry) { described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) } + + context 'when there are no angle brackets in the plural_id' do + let(:data) { { msgid_plural: 'String with no brackets' } } + + it 'returns false' do + expect(entry.plural_id_contains_potential_html?).to be_falsey + end + end + + context 'when there are angle brackets in the plural_id' do + let(:data) { { msgid_plural: 'This string has a <strong>' } } + + it 'returns true' do + expect(entry.plural_id_contains_potential_html?).to be_truthy + end + end + end + + describe '#translations_contain_potential_html?' do + subject(:entry) { described_class.new(entry_data: data, nplurals: 2, html_allowed: nil) } + + context 'when there are no angle brackets in the translations' do + let(:data) { { msgstr: 'This string has no angle brackets' } } + + it 'returns false' do + expect(entry.translations_contain_potential_html?).to be_falsey + end + end + + context 'when there are angle brackets in the translations' do + let(:data) { { msgstr: 'This string has a <strong>' } } + + it 'returns true' do + expect(entry.translations_contain_potential_html?).to be_truthy + end + end + end + + describe '#msgid_html_allowed?' do + subject(:entry) do + described_class.new(entry_data: { msgid: 'String with a <strong>' }, nplurals: 2, html_allowed: html_todo) + end + + context 'when the html in the string is in the todolist' do + let(:html_todo) { { 'plural_id' => nil, 'translations' => [] } } + + it 'returns true' do + expect(entry.msgid_html_allowed?).to be true + end + end + + context 'when the html in the string is not in the todolist' do + let(:html_todo) { nil } + + it 'returns false' do + expect(entry.msgid_html_allowed?).to be false + end + end + end + + describe '#plural_id_html_allowed?' do + subject(:entry) do + described_class.new(entry_data: { msgid_plural: 'String with many <strong>' }, nplurals: 2, html_allowed: html_todo) + end + + context 'when the html in the string is in the todolist' do + let(:html_todo) { { 'plural_id' => 'String with many <strong>', 'translations' => [] } } + + it 'returns true' do + expect(entry.plural_id_html_allowed?).to be true + end + end + + context 'when the html in the string is not in the todolist' do + let(:html_todo) { { 'plural_id' => 'String with some <strong>', 'translations' => [] } } + + it 'returns false' do + expect(entry.plural_id_html_allowed?).to be false + end + end + end + + describe '#translations_html_allowed?' do + subject(:entry) do + described_class.new(entry_data: { msgstr: 'String with a <strong>' }, nplurals: 2, html_allowed: html_todo) + end + + context 'when the html in the string is in the todolist' do + let(:html_todo) { { 'plural_id' => nil, 'translations' => ['String with a <strong>'] } } + + it 'returns true' do + expect(entry.translations_html_allowed?).to be true + end + end + + context 'when the html in the string is not in the todolist' do + let(:html_todo) { { 'plural_id' => nil, 'translations' => ['String with a different <strong>'] } } + + it 'returns false' do + expect(entry.translations_html_allowed?).to be false + end + end + + context 'when the todolist only has the msgid' do + let(:html_todo) { { 'plural_id' => nil, 'translations' => nil } } + + it 'returns false' do + expect(entry.translations_html_allowed?).to be false + end + end + end end |