diff --git a/lib/open_food_network/enterprise_issue_validator.rb b/lib/open_food_network/enterprise_issue_validator.rb index 885905f32b..b845046f2a 100644 --- a/lib/open_food_network/enterprise_issue_validator.rb +++ b/lib/open_food_network/enterprise_issue_validator.rb @@ -1,7 +1,6 @@ module OpenFoodNetwork class EnterpriseIssueValidator include Rails.application.routes.url_helpers - include Spree::TestingSupport::UrlHelpers def initialize(enterprise) @enterprise = enterprise @@ -13,14 +12,14 @@ module OpenFoodNetwork unless shipping_methods_ok? issues << { description: I18n.t('admin.enterprise_issues.has_no_shipping_methods', enterprise: @enterprise.name), - link: "#{I18n.t('admin.enterprise_issues.create_new')}" + link: "#{I18n.t('admin.enterprise_issues.create_new')}" } end unless payment_methods_ok? issues << { description: I18n.t('admin.enterprise_issues.has_no_payment_methods', enterprise: @enterprise.name), - link: "#{I18n.t('admin.enterprise_issues.create_new')}" + link: "#{I18n.t('admin.enterprise_issues.create_new')}" } end @@ -64,5 +63,9 @@ module OpenFoodNetwork @enterprise.payment_methods.available.any? end + + def spree_routes_helper + Spree::Core::Engine.routes.url_helpers + end end end diff --git a/spec/controllers/spree/admin/users_controller_spec.rb b/spec/controllers/spree/admin/users_controller_spec.rb index 22c0bc71ab..f67417e1fe 100644 --- a/spec/controllers/spree/admin/users_controller_spec.rb +++ b/spec/controllers/spree/admin/users_controller_spec.rb @@ -1,5 +1,4 @@ require 'spec_helper' -require 'spree/testing_support/bar_ability' describe Spree::Admin::UsersController do context '#authorize_admin' do @@ -34,18 +33,31 @@ describe Spree::Admin::UsersController do expect(response).to redirect_to(spree.edit_admin_user_path(test_user)) end - it 'should deny access to users with an bar role' do - user.spree_roles << Spree::Role.find_or_create_by(name: 'bar') - Spree::Ability.register_ability(BarAbility) - spree_post :index - expect(response).to redirect_to('/unauthorized') - end + describe "with BarAbility" do + class BarAbility + include CanCan::Ability - it 'should deny access to users with an bar role' do - user.spree_roles << Spree::Role.find_or_create_by(name: 'bar') - Spree::Ability.register_ability(BarAbility) - spree_post :update, id: '9' - expect(response).to redirect_to('/unauthorized') + def initialize(user) + user ||= Spree::User.new + return unless user.has_spree_role?('bar') + + can [:admin, :index, :show], Spree::Order + end + end + + it 'should deny access to users with an bar role' do + user.spree_roles << Spree::Role.find_or_create_by(name: 'bar') + Spree::Ability.register_ability(BarAbility) + spree_post :index + expect(response).to redirect_to('/unauthorized') + end + + it 'should deny access to users with an bar role' do + user.spree_roles << Spree::Role.find_or_create_by(name: 'bar') + Spree::Ability.register_ability(BarAbility) + spree_post :update, id: '9' + expect(response).to redirect_to('/unauthorized') + end end it 'should deny access to users without an admin role' do diff --git a/spec/features/admin/configuration/states_spec.rb b/spec/features/admin/configuration/states_spec.rb index 422cabe745..115740d0f7 100755 --- a/spec/features/admin/configuration/states_spec.rb +++ b/spec/features/admin/configuration/states_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' describe "States" do include AuthenticationHelper + include WebHelper let!(:country) { create(:country) } diff --git a/spec/features/admin/configuration/tax_categories_spec.rb b/spec/features/admin/configuration/tax_categories_spec.rb index 9e5d2f3823..4ab3ab8956 100644 --- a/spec/features/admin/configuration/tax_categories_spec.rb +++ b/spec/features/admin/configuration/tax_categories_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' describe "Tax Categories" do include AuthenticationHelper + include WebHelper before(:each) do login_as_admin_and_visit spree.edit_admin_general_settings_path @@ -13,9 +14,9 @@ describe "Tax Categories" do click_link "Tax Categories" expect(page).to have_content("Listing Tax Categories") within_row(1) do - expect(column_text(1)).to eq("Clothing") - expect(column_text(2)).to eq("For Clothing") - expect(column_text(3)).to eq("False") + expect(find("td:nth-child(1)").text).to eq("Clothing") + expect(find("td:nth-child(2)").text).to eq("For Clothing") + expect(find("td:nth-child(3)").text).to eq("False") end end end @@ -44,7 +45,7 @@ describe "Tax Categories" do it "should be able to update an existing tax category" do create(:tax_category) click_link "Tax Categories" - within_row(1) { click_icon :edit } + within_row(1) { find(".icon-edit").click } fill_in "tax_category_description", with: "desc 99" click_button "Update" expect(page).to have_content("successfully updated!") diff --git a/spec/features/admin/configuration/taxonomies_spec.rb b/spec/features/admin/configuration/taxonomies_spec.rb index f26b320be3..e4c4d5da69 100644 --- a/spec/features/admin/configuration/taxonomies_spec.rb +++ b/spec/features/admin/configuration/taxonomies_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' describe "Taxonomies" do include AuthenticationHelper + include WebHelper before(:each) do login_as_admin_and_visit spree.edit_admin_general_settings_path @@ -43,7 +44,7 @@ describe "Taxonomies" do it "should allow an admin to update an existing taxonomy" do create(:taxonomy) click_link "Taxonomies" - within_row(1) { click_icon :edit } + within_row(1) { find(".icon-edit").click } fill_in "taxonomy_name", with: "sports 99" click_button "Update" expect(page).to have_content("successfully updated!") diff --git a/spec/features/admin/configuration/zones_spec.rb b/spec/features/admin/configuration/zones_spec.rb index 1c03ff0941..f96229a1db 100644 --- a/spec/features/admin/configuration/zones_spec.rb +++ b/spec/features/admin/configuration/zones_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' describe "Zones" do include AuthenticationHelper + include WebHelper before do login_as_admin diff --git a/spec/features/admin/enterprise_groups_spec.rb b/spec/features/admin/enterprise_groups_spec.rb index ff19b44f17..5793080b46 100644 --- a/spec/features/admin/enterprise_groups_spec.rb +++ b/spec/features/admin/enterprise_groups_spec.rb @@ -33,8 +33,8 @@ feature ' fill_in 'enterprise_group_name', with: 'EGEGEG' fill_in 'enterprise_group_description', with: 'This is a description' check 'enterprise_group_on_front_page' - select2_search e1.name, from: 'Enterprises' - select2_search e2.name, from: 'Enterprises' + targetted_select2_search e1.name, from: '#s2id_enterprise_group_enterprise_ids' + targetted_select2_search e2.name, from: '#s2id_enterprise_group_enterprise_ids' click_link 'Contact' fill_in 'enterprise_group_address_attributes_phone', with: '000' fill_in 'enterprise_group_address_attributes_address1', with: 'My Street' diff --git a/spec/features/admin/orders_spec.rb b/spec/features/admin/orders_spec.rb index 8510ff5943..42d918db22 100644 --- a/spec/features/admin/orders_spec.rb +++ b/spec/features/admin/orders_spec.rb @@ -105,7 +105,7 @@ feature ' uncheck 'Only show complete orders' page.find('a.icon-search').click - click_icon :edit + find(".icon-edit").click expect(page).to have_current_path spree.edit_admin_order_path(incomplete_order) end diff --git a/spec/features/admin/overview_spec.rb b/spec/features/admin/overview_spec.rb index 3a4a3feb7d..d7e5ef7d6d 100644 --- a/spec/features/admin/overview_spec.rb +++ b/spec/features/admin/overview_spec.rb @@ -6,7 +6,6 @@ feature ' ', js: true do include WebHelper include AuthenticationHelper - include ::Spree::TestingSupport::AuthorizationHelpers context "as an enterprise user" do before do diff --git a/spec/features/admin/subscriptions_spec.rb b/spec/features/admin/subscriptions_spec.rb index 4278018aa2..30c27bb2d7 100644 --- a/spec/features/admin/subscriptions_spec.rb +++ b/spec/features/admin/subscriptions_spec.rb @@ -534,7 +534,7 @@ feature 'Subscriptions' do def add_variant_to_subscription(variant, quantity) row_count = all("#subscription-line-items .item").length variant_name = variant.full_name.present? ? "#{variant.name} - #{variant.full_name}" : variant.name - select2_search variant.name, from: I18n.t(:name_or_sku), dropdown_css: ".select2-drop", select_text: variant_name + targetted_select2_search variant.name, from: "#s2id_add_variant_id", dropdown_css: ".select2-drop", select_text: variant_name fill_in "add_quantity", with: quantity click_link "Add" expect(page).to have_selector("#subscription-line-items .item", count: row_count + 1) diff --git a/spec/lib/open_food_network/enterprise_issue_validator_spec.rb b/spec/lib/open_food_network/enterprise_issue_validator_spec.rb index 8503da23bb..c7ee1bfbe7 100644 --- a/spec/lib/open_food_network/enterprise_issue_validator_spec.rb +++ b/spec/lib/open_food_network/enterprise_issue_validator_spec.rb @@ -1,3 +1,4 @@ +require 'spec_helper' require 'open_food_network/enterprise_issue_validator' module OpenFoodNetwork diff --git a/spec/lib/spree/i18n_spec.rb b/spec/lib/spree/i18n_spec.rb index 386dd9d2ec..397dd36641 100644 --- a/spec/lib/spree/i18n_spec.rb +++ b/spec/lib/spree/i18n_spec.rb @@ -2,7 +2,7 @@ require 'rspec/expectations' require 'spree/i18n' -require 'spree/testing_support/i18n' +require 'support/i18n_translations_checker' describe "i18n" do before do diff --git a/spec/lib/spree/money_spec.rb b/spec/lib/spree/money_spec.rb index 139806137c..12b1d975b8 100644 --- a/spec/lib/spree/money_spec.rb +++ b/spec/lib/spree/money_spec.rb @@ -3,6 +3,8 @@ require 'spec_helper' describe Spree::Money do + include PreferencesHelper + before do configure_spree_preferences do |config| config.currency = "USD" diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d4130a9d2d..f1111e7576 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -32,8 +32,7 @@ Shoulda::Matchers.configure do |config| end end -# Allow connections to phantomjs/selenium whilst raising errors -# when connecting to external sites +# Allow connections to selenium whilst raising errors when connecting to external sites require 'webmock/rspec' WebMock.enable! WebMock.disable_net_connect!( @@ -44,10 +43,7 @@ WebMock.disable_net_connect!( # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. Dir[Rails.root.join("spec/support/**/*.rb")].sort.each { |f| require f } -require 'spree/testing_support/capybara_ext' require 'spree/api/testing_support/setup' -require 'spree/testing_support/authorization_helpers' -require 'spree/testing_support/preferences' require 'support/api_helper' # Capybara config @@ -65,6 +61,11 @@ end Capybara.default_max_wait_time = 30 +Capybara.configure do |config| + config.match = :prefer_exact + config.ignore_hidden_elements = true +end + require "paperclip/matchers" # Override setting in Spree engine: Spree::Core::MailSettings @@ -155,7 +156,7 @@ RSpec.configure do |config| config.include Spree::UrlHelpers config.include Spree::CheckoutHelpers config.include Spree::MoneyHelper - config.include Spree::TestingSupport::Preferences + config.include PreferencesHelper config.include ControllerRequestsHelper, type: :controller config.include Devise::TestHelpers, type: :controller config.extend Spree::Api::TestingSupport::Setup, type: :controller diff --git a/spec/support/i18n_translations_checker.rb b/spec/support/i18n_translations_checker.rb new file mode 100644 index 0000000000..a722f13302 --- /dev/null +++ b/spec/support/i18n_translations_checker.rb @@ -0,0 +1,98 @@ +# frozen_string_literal: true + +# This file exists solely to test whether or not there are missing translations +# within the code that Spree's test suite covers. +# +# If there is a translation referenced which has no corresponding key within the +# .yml file, then there will be a message output at the end of the suite showing +# that. +# +# If there is a translation within the locale file which *isn't* used in the +# test, this will also be shown at the end of the suite run. +module Spree + class << self + attr_accessor :used_translations, :missing_translation_messages, + :unused_translations, :unused_translation_messages + alias_method :normal_t, :t + end + + def self.t(*args) + original_args = args.dup + options = args.extract_options! + self.used_translations ||= [] + [*args.first].each do |translation_key| + key = ([*options[:scope]] << translation_key).join('.') + self.used_translations << key + end + normal_t(*original_args) + end + + def self.check_missing_translations + self.missing_translation_messages = [] + self.used_translations ||= [] + used_translations.map { |a| a.split('.') }.each do |translation_keys| + root = translations + processed_keys = [] + translation_keys.each do |key| + begin + root = root.fetch(key.to_sym) + processed_keys << key.to_sym + rescue KeyError + error = "#{(processed_keys << key).join('.')} (#{I18n.locale})" + unless Spree.missing_translation_messages.include?(error) + Spree.missing_translation_messages << error + end + end + end + end + end + + def self.check_unused_translations + self.used_translations ||= [] + self.unused_translation_messages = [] + self.unused_translations = [] + load_translations(translations) + translation_diff = unused_translations - used_translations + translation_diff.each do |translation| + Spree.unused_translation_messages << "#{translation} (#{I18n.locale})" + end + end + + def self.load_translations(hash, root = []) + hash.each do |k, v| + if v.is_a?(Hash) + load_translations(v, root.dup << k) + else + key = (root + [k]).join('.') + unused_translations << key + end + end + end + private_class_method :load_translations + + def self.translations + @translations ||= I18n.backend.__send__(:translations)[I18n.locale][:spree] + end + private_class_method :translations +end + +RSpec.configure do |config| + # Need to check here again because this is used in i18n_spec too. + if ENV['CHECK_TRANSLATIONS'] + config.after :suite do + Spree.check_missing_translations + if Spree.missing_translation_messages.any? + puts "\nThere are missing translations within Spree:" + puts Spree.missing_translation_messages.sort + exit(1) + end + + Spree.check_unused_translations + if Spree.unused_translation_messages.any? + puts "\nThere are unused translations within Spree:" + puts Spree.unused_translation_messages.sort + exit(1) + end + end + end +end diff --git a/spec/support/matchers/select2_matchers.rb b/spec/support/matchers/select2_matchers.rb index ff7b6b8afe..d6acd7eddc 100644 --- a/spec/support/matchers/select2_matchers.rb +++ b/spec/support/matchers/select2_matchers.rb @@ -6,7 +6,6 @@ RSpec::Matchers.define :have_select2 do |id, options = {}| match do |node| @id, @options, @node = id, options, node - # id = find_label_by_text(locator) from = "#s2id_#{id}" results = [] @@ -35,7 +34,6 @@ RSpec::Matchers.define :have_select2 do |id, options = {}| match_when_negated do |node| @id, @options, @node = id, options, node - # id = find_label_by_text(locator) from = "#s2id_#{id}" results = [] diff --git a/spec/support/preferences_helper.rb b/spec/support/preferences_helper.rb new file mode 100644 index 0000000000..b69f5e8215 --- /dev/null +++ b/spec/support/preferences_helper.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module PreferencesHelper + # Resets all preferences to default values, you can + # pass a block to override the defaults with a block + # + # reset_spree_preferences do |config| + # config.site_name = "my fancy pants store" + # end + # + def reset_spree_preferences(&config_block) + Spree::Preferences::Store.instance.persistence = false + Spree::Preferences::Store.instance.clear_cache + + config = Rails.application.config.spree.preferences + configure_spree_preferences(&config_block) if block_given? + end + + def configure_spree_preferences + config = Rails.application.config.spree.preferences + yield(config) if block_given? + end +end diff --git a/spec/support/request/web_helper.rb b/spec/support/request/web_helper.rb index 25ae07bbc1..a117b7742a 100644 --- a/spec/support/request/web_helper.rb +++ b/spec/support/request/web_helper.rb @@ -95,16 +95,29 @@ module WebHelper end end + def within_row(num, &block) + within("table.index tbody tr:nth-child(#{num})", &block) + end + def select2_select(value, options) id = options[:from] options[:from] = "#s2id_#{id}" targetted_select2(value, options) end + def targetted_select2(value, options) + # find select2 element and click it + find(options[:from]).find('a').click + select_select2_result(value) + end + + def select_select2_result(value) + sleep(1) + page.execute_script(%Q{$("div.select2-result-label:contains('#{value}')").mouseup()}) + end + # Support having different texts to search for and to click in the select2 # field. - # - # This overrides the method in Spree. def targetted_select2_search(value, options) page.execute_script %{$('#{options[:from]}').select2('open')} page.execute_script "$('#{options[:dropdown_css]} input.select2-input').val('#{value}').trigger('keyup-change');" @@ -124,25 +137,8 @@ module WebHelper page.execute_script "jQuery('#{selector}').select2('close');" end - def select2_search_async(value, options) - id = find_label_by_text(options[:from]) - options[:from] = "#s2id_#{id}" - targetted_select2_search_async(value, options) - end - - def targetted_select2_search_async(value, options) - page.execute_script %{$('#{options[:from]}').select2('open')} - page.execute_script "$('#{options[:dropdown_css]} input.select2-input').val('#{value}').trigger('keyup-change');" - select_select2_result_async(value) - end - - def select_select2_result_async(value) - while page.has_selector? "div.select2-searching" - return if page.has_selector? "div.select2-no-results" - - sleep 0.2 - end - page.execute_script(%{$("div.select2-result-label:contains('#{value}')").mouseup()}) + def set_select2_field(field, value) + page.execute_script %Q{$('#{field}').select2('val', '#{value}')} end def accept_js_alert