From ca256f28a7388728e987d42fbad393bb2ca7b02c Mon Sep 17 00:00:00 2001 From: Markus Kahl Date: Thu, 8 May 2014 15:05:47 +0100 Subject: [PATCH] I18n support for cukes. Use i18n for labels login, register and 'forgot password'. --- config/initializers/i18n_in_cukes.rb | 89 ++++++++++++++++++++++++ config/locales/en.yml | 6 +- features/session/user_session.feature | 4 +- features/support/login_steps.rb | 2 +- features/users/lost_password.feature | 2 +- features/users/user_auth.feature | 8 +-- features/users/user_registration.feature | 2 +- 7 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 config/initializers/i18n_in_cukes.rb diff --git a/config/initializers/i18n_in_cukes.rb b/config/initializers/i18n_in_cukes.rb new file mode 100644 index 0000000000..b4b18db01b --- /dev/null +++ b/config/initializers/i18n_in_cukes.rb @@ -0,0 +1,89 @@ +#-- encoding: UTF-8 +#-- copyright +# OpenProject is a project management system. +# Copyright (C) 2012-2014 the OpenProject Foundation (OPF) +# +# 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 doc/COPYRIGHT.rdoc for more details. +#++ + +## +# The following bit of code patches rspec to accept i18n codes in all finders and when looking for text. +# This way labels don't have to be hardcoded which means they can be changed without breaking tests. +# Examples: +# +# Given I click on "t:account.delete" +# When I follow "t:label_register" +# And I press "t:button_login" +# +# Where the i18n file contains the following: +# +# en: +# label_register: "Sign up" +# label_button_login: "Sign in" +# +# account: +# delete: "Delete account" +# +if Rails.env.test? + require 'capybara' + require 'capybara/rspec/matchers' + + Capybara::Node::Finders.module_eval do + def find_with_i18n(*args) + i18n = args[1] + if args.size >= 2 && i18n.is_a?(String) && i18n =~ /^t:[^\s]/ + begin + args[1] = I18n.t(i18n.split(":").last) + find_without_i18n(*args) + rescue Capybara::ElementNotFound + # perhaps it was not intended to be an i18n code after all + args[1] = i18n + find_without_i18n(*args) + end + else + find_without_i18n(*args) + end + end + + alias_method_chain :find, :i18n + end + + Capybara::RSpecMatchers::HaveText.module_eval do + def matches_with_i18n?(actual) + if content.is_a?(String) && content =~ /^t:[^\s]/ + i18n = content + @content = I18n.t(i18n.split(":").last) + matches_without_i18n?(actual) || begin + # perhaps it was not intended to be an i18n code after all + @content = i18n + matches_without_i18n?(actual) + end + else + matches_without_i18n?(actual) + end + end + + alias_method_chain :matches?, :i18n + end +end diff --git a/config/locales/en.yml b/config/locales/en.yml index 88a4a83631..ed6cb4f42e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -359,7 +359,7 @@ en: button_list: "List" button_lock: "Lock" button_log_time: "Log time" - button_login: "Login" + button_login: "Sign in" button_move: "Move" button_move_and_follow: "Move and follow" button_quote: "Quote" @@ -804,7 +804,7 @@ en: label_overall_activity: "Overall activity" label_overall_spent_time: "Overall spent time" label_overview: "Overview" - label_password_lost: "Lost password" + label_password_lost: "Forgot your password?" label_password_rule_lowercase: "Lowercase" label_password_rule_numeric: "Numeric Characters" label_password_rule_special: "Special Characters" @@ -838,7 +838,7 @@ en: label_query_plural: "Custom queries" label_query_menu_item: "Query menu item" label_read: "Read..." - label_register: "Register" + label_register: "Create a new account" label_register_with_developer: "Register as developer" label_registered_on: "Registered on" label_registration_activation_by_email: "account activation by email" diff --git a/features/session/user_session.feature b/features/session/user_session.feature index 8f5cefcd47..e069f525e4 100644 --- a/features/session/user_session.feature +++ b/features/session/user_session.feature @@ -36,7 +36,7 @@ Feature: User session Then I should be on the login page When I fill in "bob" for "username" within "#login-form" And I fill in "adminADMIN!" for "password" within "#login-form" - And I click on "Login" within "#login-form" + And I click on "t:button_login" within "#login-form" And I go to the my account page Then I should be on the my account page @@ -45,7 +45,7 @@ Feature: User session Then I should be on the login page When I fill in "bob" for "username" within "#login-form" And I fill in "adminADMIN!" for "password" within "#login-form" - And I click on "Login" within "#login-form" + And I click on "t:button_login" within "#login-form" Then I should be on the my account page Scenario: Autologin works if enabled diff --git a/features/support/login_steps.rb b/features/support/login_steps.rb index 91c05f6315..25c1ea03aa 100644 --- a/features/support/login_steps.rb +++ b/features/support/login_steps.rb @@ -34,7 +34,7 @@ module LoginSteps within('#login-form') do fill_in User.human_attribute_name(:login), :with => login fill_in 'Password', :with => password - click_button 'Login' + click_button 't:button_login' end end end diff --git a/features/users/lost_password.feature b/features/users/lost_password.feature index 109dde66a3..71879065d1 100644 --- a/features/users/lost_password.feature +++ b/features/users/lost_password.feature @@ -37,7 +37,7 @@ Feature: Lost Password Scenario: Set a new password using lost password link And I am on the login page - When I follow "Lost password" within "#login-form" + When I follow "t:label_password_lost" within "#login-form" And I fill in "johndoe@example.com" for "Email" And I press "Submit" Then I should see "has been sent to you" diff --git a/features/users/user_auth.feature b/features/users/user_auth.feature index 01d67b6530..61194bb19a 100644 --- a/features/users/user_auth.feature +++ b/features/users/user_auth.feature @@ -49,7 +49,7 @@ Feature: User Authentication -@javascript +@javascript Scenario: A user gets a error message if the false credentials are filled in Given I am logged in as "joe" Then I should see "Invalid user or password" @@ -58,14 +58,14 @@ Then I should see "Invalid user or password" Scenario: A user is able to login successfully with provided credentials Given I am on the login page And I am admin -Then I should see "Admin" within "#top-menu-items" +Then I should see "Admin" within "#top-menu-items" @javascript Scenario: Lost password notification mail will not be sent in case incorrect mail is given Given I am on the login page And I open the "Openproject Admin" menu -And I follow "Lost password" within "#login-form" +And I follow "t:label_password_lost" within "#login-form" Then I should be on the lost password page And I fill in "mail" with "bilbo@shire.com" And I click on "Submit" -Then I should see "Unknown user" \ No newline at end of file +Then I should see "Unknown user" diff --git a/features/users/user_registration.feature b/features/users/user_registration.feature index e5ec83aa1c..50a59f8dd9 100644 --- a/features/users/user_registration.feature +++ b/features/users/user_registration.feature @@ -51,7 +51,7 @@ Feature: User Registration Background: Given I am on the homepage And I open the "Sign in" menu -And I follow "Register" within "#top-menu-items" +And I follow "t:label_register" within "#top-menu-items" Then I should be on the registration page Scenario: A user can register successfully after filling in the registration form