OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
# frozen_string_literal: true
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++
require "spec_helper"
module OpenProject
RSpec.describe I18n, type: :helper do
include Redmine::I18n
let(:format) { "%d/%m/%Y" }
let(:user) { build_stubbed(:user) }
describe "#format_date with time" do
current_user { build_stubbed(:user, preferences: { time_zone: user_time_zone }) }
describe "with user time zone" do
let(:user_time_zone) { "Europe/Athens" }
it "returns a date string in the user timezone for a utc timestamp" do
time = ActiveSupport::TimeZone["UTC"].local(2013, 6, 30, 23, 59)
expect(format_date(time, format:)).to eq "01/07/2013"
end
it "returns a date string in the user timezone for a non-utc timestamp" do
time = ActiveSupport::TimeZone["Berlin"].local(2013, 6, 30, 23, 59)
expect(format_date(time, format:)).to eq "01/07/2013"
end
end
describe "without user time zone" do
let(:user_time_zone) { "" }
it "returns a date string in the utc timezone for a utc timestamp" do
time = ActiveSupport::TimeZone["UTC"].local(2013, 6, 30, 23, 59)
expect(format_date(time, format:)).to eq "30/06/2013"
end
it "returns a date string in the utc timezone for a non-utc timestamp" do
time = ActiveSupport::TimeZone["Berlin"].local(2013, 6, 30, 23, 59)
expect(format_date(time, format:)).to eq "30/06/2013"
end
end
end
describe "all_languages" do
# Those are the two languages we support
it "includes en" do
expect(all_languages).to include("en")
end
it "includes de" do
expect(all_languages).to include("de")
end
it "returns no js language as they are duplicates of the rest of the other language" do
expect(all_languages).not_to(be_any { |l| l.to_s.start_with?("js-") })
end
# it is OK if more languages exist
it "has multiple languages" do
expect(all_languages).to include "en", "de", "fr", "es"
expect(all_languages.size).to be >= 25
end
end
describe "valid_languages" do
it "allows languages that are available" do
with_settings(available_languages: ["en"])
expect(valid_languages).to eq ["en"]
end
it "allows language which is not in available languages list but is the default language" do
with_settings(available_languages: ["en"], default_language: "fr")
expect(valid_languages).to eq ["en", "fr"]
end
it "allows only languages that exist" do
with_settings(available_languages: ["en", "de", "klingon"])
expect(valid_languages).to contain_exactly("en", "de")
end
it "is sorted alphabetically" do
with_settings(available_languages: ["de", "en"], default_language: "fr")
expect(valid_languages).to eq(valid_languages.sort)
with_settings(available_languages: ["en", "fr"], default_language: "de")
expect(valid_languages).to eq(valid_languages.sort)
end
end
describe "set_language_if_valid" do
before do
allow(described_class).to receive(:locale=)
end
context "with all supported languages available" do
before do
with_settings(available_languages: Redmine::I18n.all_languages)
end
Setting.all_languages.each do |lang|
it "sets I18n.locale to #{lang.inspect}" do
set_language_if_valid(lang.to_s)
set_language_if_valid(lang.to_sym)
expect(described_class).to have_received(:locale=).with(lang.to_s).twice
end
end
end
it "does not set I18n.locale to an unavailable language" do
with_settings(available_languages: ["en"])
set_language_if_valid("de")
set_language_if_valid(:de)
expect(described_class).not_to have_received(:locale=).with(:de)
expect(described_class).not_to have_received(:locale=).with("de")
end
end
describe "find_language" do
before do
with_settings(available_languages: ["de"], default_language: "en")
end
it "is nil if language is not available nor the default language" do
expect(find_language(:fr)).to be_nil
end
it "is nil if no language is given" do
expect(find_language("")).to be_nil
expect(find_language(nil)).to be_nil
end
it "is the language if it is in available languages" do
expect(find_language(:de)).to eq "de"
expect(find_language("de")).to eq "de"
end
it "is the language if it is the default language" do
expect(find_language(:en)).to eq "en"
expect(find_language("en")).to eq "en"
end
it "can be found by uppercase" do
expect(find_language(:DE)).to eq "de"
expect(find_language("DE")).to eq "de"
expect(find_language(:EN)).to eq "en"
expect(find_language("EN")).to eq "en"
end
it "is nil if non valid string is passed" do
expect(find_language("*")).to be_nil
expect(find_language("78445")).to be_nil
expect(find_language("/)(")).to be_nil
end
end
describe "link_translation" do
let(:urls) do
{ url_1: "http://openproject.com/foo", url_2: "/baz" }
end
before do
allow(::I18n)
.to receive(:t)
.with("translation_with_a_link")
.and_return("There is a [link](url_1) in this translation! Maybe even [two](url_2)?")
end
it "allows to insert links into translations" do
translated = link_translate :translation_with_a_link, links: urls, external: false
expect(translated).to eq(
'There is a <a href="http://openproject.com/foo" data-view-component="true" class="Link Link--underline">link</a>' +
' in this translation! Maybe even <a href="/baz" data-view-component="true" class="Link Link--underline">two</a>?'
)
end
context "when passing URLs as a list of symbols" do
let(:urls) do
{ url_1: [:a, :b], url_2: [:a, :c] }
end
before do
allow(OpenProject::Static::Links).to receive(:url_for).and_return("/no-args")
allow(OpenProject::Static::Links).to receive(:url_for).with(:a, :b).and_return("https://example.com/a-b")
allow(OpenProject::Static::Links).to receive(:url_for).with(:a, :c).and_return("/a-c")
end
it "resolves the links from static links" do
translated = link_translate :translation_with_a_link, links: urls, external: false
expect(translated).to eq(
'There is a <a href="https://example.com/a-b" data-view-component="true" class="Link Link--underline">link</a>' +
' in this translation! Maybe even <a href="/a-c" data-view-component="true" class="Link Link--underline">two</a>?'
)
end
end
end
describe "#format_date" do
context "without a date_format setting", with_settings: { date_format: "" } do
it "uses the locale formate" do
expect(format_date(Date.today))
.to eql described_class.l(Date.today)
end
end
context "with a date_format setting", with_settings: { date_format: "%d %m %Y" } do
it "adheres to the format" do
expect(format_date(Date.today))
.to eql Date.today.strftime("%d %m %Y")
end
end
valid_languages.each do |lang|
context "for lang #{lang}" do
it "raises no error" do
described_class.with_locale lang do
expect { format_date(Date.today) }
.not_to raise_error
end
end
end
end
end
describe "#numer_to_human_size" do
valid_languages.each do |lang|
context "for locale #{lang}" do
it "does not raise an error" do
described_class.with_locale lang do
expect { number_to_human_size(1024 * 1024 * 4) }
.not_to raise_error
end
end
end
end
end
describe "#format_time", with_settings: {
time_format: "%H %M",
date_format: "%d %m %Y"
} do
let(:user_time_zone) { "" }
let(:now) { Time.zone.parse("2011-02-20 15:45:22") }
current_user { build_stubbed(:user, preferences: { time_zone: user_time_zone }) }
it "with date and hours" do
expect(format_time(now))
.to eql now.strftime("%d %m %Y %H %M")
end
it "with only hours" do
expect(format_time(now, include_date: false))
.to eql now.strftime("%H %M")
end
it "renders correctly for only hours and when providing a custom format" do
expect(format_time(now, include_date: false, format: "%H:%M"))
.to eql now.strftime("%H:%M")
end
context "with another time zone configured for the user" do
# Kathmandu has a +05:45 offset
let(:user_time_zone) { "Kathmandu" }
it "renders correctly for data and hours" do
expect(format_time(now))
.to eql "20 02 2011 21 30"
end
it "renders correctly for only hours" do
expect(format_time(now, include_date: false))
.to eql "21 30"
end
it "renders correctly for only hours and when providing a custom format" do
expect(format_time(now, include_date: false, format: "%H:%M"))
.to eql "21:30"
end
end
context "with a different format defined", with_settings: {
time_format: "%H:%M",
date_format: "%Y-%m-%d"
} do
it "renders date and hours" do
expect(format_time(now))
.to eql "2011-02-20 15:45"
end
it "renders only hours" do
expect(format_time(now, include_date: false))
.to eql "15:45"
end
end
context "without time and date format", with_settings: {
time_format: "",
date_format: ""
} do
it "falls back to default for date and hours" do
expect(format_time(now))
.to eql "02/20/2011 03:45 PM"
end
it "falls back to default for only hours" do
expect(format_time(now, include_date: false))
.to eql "03:45 PM"
end
valid_languages.each do |lang|
context "for lang #{lang}" do
it "raises no error for date and hours" do
described_class.with_locale lang do
expect { format_time(now) }
.not_to raise_error
end
end
it "raises no error for only hours" do
described_class.with_locale lang do
expect { format_time(now, include_date: false) }
.not_to raise_error
end
end
end
end
end
context "without time format", with_settings: {
time_format: "",
date_format: "%Y-%m-%d"
} do
it "falls back to default for date and hours" do
expect(format_time(now))
.to eql "2011-02-20 03:45 PM"
end
it "falls back to default for only hours" do
expect(format_time(now, include_date: false))
.to eql "03:45 PM"
end
end
context "without date format", with_settings: {
time_format: "%H:%M",
date_format: ""
} do
it "falls back to default for date and hours" do
expect(format_time(now))
.to eql "02/20/2011 15:45"
end
it "falls back to default for only hours" do
expect(format_time(now, include_date: false))
.to eql "15:45"
end
end
end
describe "#formatted_time_zone_offset" do
current_user { build_stubbed(:user, preferences: { time_zone: user_time_zone }) }
let(:user_time_zone) { "" }
let(:berlin_gmt) { ActiveSupport::TimeZone["Europe/Berlin"].now.utc_offset == 7200 ? "UTC+02:00" : "UTC+01:00" }
context "with the current user having set a time zone" do
let(:user_time_zone) { "Europe/Berlin" }
it "renders the time zone of the user" do
expect(formatted_time_zone_offset).to eql berlin_gmt
end
end
context "without the current user having a time zone and no default one configured" do
let(:user_time_zone) { "" }
it "renders the UTC time zone" do
expect(formatted_time_zone_offset).to eql "UTC+00:00"
end
end
context "without the current user having a time zone but with a default one configured",
with_settings: { user_default_timezone: "Europe/Berlin" } do
let(:user_time_zone) { "" }
it "renders the default time zone" do
expect(formatted_time_zone_offset).to eql berlin_gmt
end
end
context "without the current user having a time zone and also a default one configured",
with_settings: { user_default_timezone: "America/Atlanta" } do
let(:user_time_zone) { "Europe/Berlin" }
it "renders the default time zone" do
expect(formatted_time_zone_offset).to eql berlin_gmt
end
end
end
describe "day names" do
valid_languages.each do |lang|
context "for locale #{lang}" do
it "is an array" do
described_class.with_locale lang do
expect(described_class.t("date.day_names"))
.to be_a Array
end
end
it "has 7 elements" do
described_class.with_locale lang do
expect(described_class.t("date.day_names").size)
.to eq 7
end
end
end
end
end
describe "month names" do
valid_languages.each do |lang|
context "for locale #{lang}" do
it "is an array" do
described_class.with_locale lang do
expect(described_class.t("date.month_names"))
.to be_a Array
end
end
it "has 13 elements" do
described_class.with_locale lang do
expect(described_class.t("date.month_names").size)
.to eq 13
end
end
end
end
end
describe ".l" do
valid_languages.each do |lang|
context "for locale #{lang}" do
it "is not 'default' for a date" do
described_class.with_locale lang do
expect(described_class.l(Date.today, format: :default))
.not_to eq "default"
end
end
it "is not 'default' for a time" do
described_class.with_locale lang do
expect(described_class.l(Time.zone.now, format: :default))
.not_to eq "time"
end
end
end
end
end
end
end