From f9ba3080725e375cae2cd403ccd543d32a320820 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Thu, 20 Aug 2020 21:52:18 +0200 Subject: [PATCH] Added test_create_binding_target Goals (incl. binding) are required to have targets Plugin fixtures are available through accessors --- app/models/goal.rb | 3 +- app/views/foods/_contextual.html.erb | 2 +- app/views/goals/_show_form.html.erb | 2 +- app/views/layouts/_sidebar.html.erb | 2 +- app/views/meals/_contextual.html.erb | 2 +- app/views/measurements/_contextual.html.erb | 2 +- app/views/quantities/index.html.erb | 2 +- app/views/sources/index.html.erb | 2 +- app/views/targets/_contextual.html.erb | 2 +- app/views/targets/_form.html.erb | 1 - app/views/units/index.html.erb | 2 +- config/locales/en.yml | 4 +-- init.rb | 2 +- lib/body_tracking/plugin_fixtures_loader.rb | 34 +++++++++++++++++++-- lib/body_tracking/project_patch.rb | 4 ++- test/application_system_test_case.rb | 7 +++-- test/fixtures/enabled_modules.yml | 3 ++ test/fixtures/goals.yml | 2 +- test/fixtures/member_roles.yml | 3 ++ test/fixtures/roles.yml | 15 +++++++++ test/system/targets_test.rb | 34 ++++++++++++++++----- 21 files changed, 103 insertions(+), 27 deletions(-) create mode 100644 test/fixtures/enabled_modules.yml create mode 100644 test/fixtures/member_roles.yml create mode 100644 test/fixtures/roles.yml diff --git a/app/models/goal.rb b/app/models/goal.rb index 4e8730b..2078692 100644 --- a/app/models/goal.rb +++ b/app/models/goal.rb @@ -8,7 +8,8 @@ class Goal < ActiveRecord::Base validates :target_exposures, presence: true validates :is_binding, uniqueness: {scope: :project_id}, if: :is_binding? - validates :name, presence: true, uniqueness: {scope: :project_id} + validates :name, presence: true, uniqueness: {scope: :project_id}, + exclusion: {in: [I18n.t('targets.form.binding_goal')], unless: :is_binding?} after_initialize do if new_record? diff --git a/app/views/foods/_contextual.html.erb b/app/views/foods/_contextual.html.erb index aeef49b..e9af9b4 100644 --- a/app/views/foods/_contextual.html.erb +++ b/app/views/foods/_contextual.html.erb @@ -1,4 +1,4 @@ -<% if User.current.allowed_to?(:manage_common, @project) %> +<% if User.current.allowed_to?(:manage_body_trackers, @project) %> <%= link_to t(".link_import_foods"), '#', class: 'icon icon-multiple', onclick: '$("#import-foods").show(); $("#filename").focus(); return false;' %> <%= link_to t(".link_new_food"), new_project_food_path(@project, view: view), diff --git a/app/views/goals/_show_form.html.erb b/app/views/goals/_show_form.html.erb index 4ade2c8..9b06e3c 100644 --- a/app/views/goals/_show_form.html.erb +++ b/app/views/goals/_show_form.html.erb @@ -1,5 +1,5 @@ <%= fields_for 'goal', goal do |ff| %> - + <%= ff.select :id, options_from_collection_for_select(@project.goals, :id, :name, goal.id), {required: true}, autocomplete: 'off', diff --git a/app/views/layouts/_sidebar.html.erb b/app/views/layouts/_sidebar.html.erb index 7d083c9..eec614a 100644 --- a/app/views/layouts/_sidebar.html.erb +++ b/app/views/layouts/_sidebar.html.erb @@ -29,7 +29,7 @@
  • <%= link_to t(".link_sources"), project_sources_path(@project) %>
  • <%= link_to t(".link_quantities"), project_quantities_path(@project) %>
  • <%= link_to t(".link_units"), project_units_path(@project) %>
  • - <% if User.current.allowed_to?(:manage_common, @project) %> + <% if User.current.allowed_to?(:manage_body_trackers, @project) %>
  •  
  • <%= link_to t(".link_defaults"), defaults_project_body_trackers_path(@project), method: :post, data: {confirm: t(".confirm_defaults")} %>

  • diff --git a/app/views/meals/_contextual.html.erb b/app/views/meals/_contextual.html.erb index 15f2a49..afa408f 100644 --- a/app/views/meals/_contextual.html.erb +++ b/app/views/meals/_contextual.html.erb @@ -1,4 +1,4 @@ -<% if User.current.allowed_to?(:manage_common, @project) %> +<% if User.current.allowed_to?(:manage_body_trackers, @project) %> <%= link_to t(".link_new_meal"), new_project_meal_path(@project), {remote: true, class: 'icon icon-add'} %> <% end %> diff --git a/app/views/measurements/_contextual.html.erb b/app/views/measurements/_contextual.html.erb index b9451f1..ced8432 100644 --- a/app/views/measurements/_contextual.html.erb +++ b/app/views/measurements/_contextual.html.erb @@ -1,4 +1,4 @@ -<% if User.current.allowed_to?(:manage_common, @project) %> +<% if User.current.allowed_to?(:manage_body_trackers, @project) %> <%= link_to t(".link_new_measurement"), new_project_measurement_path(@project, @view_params), {remote: true, class: 'icon icon-add'} %> diff --git a/app/views/quantities/index.html.erb b/app/views/quantities/index.html.erb index 64aa6e4..0c4b8ce 100644 --- a/app/views/quantities/index.html.erb +++ b/app/views/quantities/index.html.erb @@ -1,5 +1,5 @@
    - <% if User.current.allowed_to?(:manage_common, @project) %> + <% if User.current.allowed_to?(:manage_body_trackers, @project) %> <%= link_to t(".link_new_quantity"), new_project_quantity_path(@project), {remote: true, class: 'icon icon-add'} %> <% end %> diff --git a/app/views/sources/index.html.erb b/app/views/sources/index.html.erb index 55b8ca9..4ff7447 100644 --- a/app/views/sources/index.html.erb +++ b/app/views/sources/index.html.erb @@ -1,5 +1,5 @@
    - <% if User.current.allowed_to?(:manage_common, @project) %> + <% if User.current.allowed_to?(:manage_body_trackers, @project) %> <%= link_to t(".link_new_source"), '#', class: 'icon icon-add', onclick: '$("#add-source").show(); $("#source_name").focus(); return false;' %> <% end %> diff --git a/app/views/targets/_contextual.html.erb b/app/views/targets/_contextual.html.erb index a6bfd4e..c11ed80 100644 --- a/app/views/targets/_contextual.html.erb +++ b/app/views/targets/_contextual.html.erb @@ -1,4 +1,4 @@ -<% if User.current.allowed_to?(:manage_common, @project) %> +<% if User.current.allowed_to?(:manage_body_trackers, @project) %> <%= link_to t(".link_new_target"), new_project_target_path(@project, @view_params), {remote: true, class: 'icon icon-add'} %> diff --git a/app/views/targets/_form.html.erb b/app/views/targets/_form.html.erb index 645706e..185de94 100644 --- a/app/views/targets/_form.html.erb +++ b/app/views/targets/_form.html.erb @@ -5,7 +5,6 @@
    <% if goal.persisted? || goal.is_binding? %>

    <%= render partial: 'goals/show_form', locals: {goal: goal} %>

    - <%#= t '.effective_from' %>

    <%= f.date_field :effective_from, disabled: !goal.is_binding? %>

    <% else %> <%= render partial: 'goals/form', locals: {goal: goal} %> diff --git a/app/views/units/index.html.erb b/app/views/units/index.html.erb index c92e9e0..8c5a4c5 100644 --- a/app/views/units/index.html.erb +++ b/app/views/units/index.html.erb @@ -1,5 +1,5 @@
    - <% if User.current.allowed_to?(:manage_common, @project) %> + <% if User.current.allowed_to?(:manage_body_trackers, @project) %> <%= link_to t(".link_new_unit"), '#', class: 'icon icon-add', onclick: '$("#add-unit").show(); $("#unit_shortname").focus(); return false;' %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 821f53b..cd05585 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -94,10 +94,10 @@ en: contextual: link_new_target: 'New target' form: - choose_quantity: "Choose quantity" + binding_goal: '- binding -' + choose_quantity: 'Choose quantity' button_new_target: 'Add target' button_delete_target: 'Delete' - effective_from: ', effective from:' new_form: heading_new_target: 'New target' index: diff --git a/init.rb b/init.rb index fff95ad..e5c3870 100644 --- a/init.rb +++ b/init.rb @@ -25,7 +25,7 @@ Redmine::Plugin.register :body_tracking do quantities: [:index, :parents, :filter], units: [:index], }, read: true - permission :manage_common, { + permission :manage_body_trackers, { body_trackers: [:defaults], goals: [:edit], targets: [:new, :create, :edit, :update, :destroy, :reapply, :toggle_exposure], diff --git a/lib/body_tracking/plugin_fixtures_loader.rb b/lib/body_tracking/plugin_fixtures_loader.rb index b381f90..2f4100c 100644 --- a/lib/body_tracking/plugin_fixtures_loader.rb +++ b/lib/body_tracking/plugin_fixtures_loader.rb @@ -2,9 +2,39 @@ module BodyTracking::PluginFixturesLoader def self.included(base) base.class_eval do def self.plugin_fixtures(*symbols) - fixtures_dir = File.expand_path('../../../test/fixtures/', __FILE__) - ActiveRecord::FixtureSet.create_fixtures(fixtures_dir, symbols) + plugin_fixtures_path = Rails.root.join('plugins', 'body_tracking', 'test', 'fixtures') + ActiveRecord::FixtureSet.create_fixtures(plugin_fixtures_path, symbols) + # ActiveRecord::TestFixtures#fixtures creates model_name(:fixture_name) accessors + fixtures *symbols end end end + + #private + + ## Load fixtures giving preference for plugin defined ones + #def load_fixtures(*args) + # # call create_fixtures directly instead of load_fixtures + # # or + # # create_fixtures in plugin_fixtures (include method in + # # ActiveRecord::TestFixtures) + load by calling #fixtures (like in issue_recurring) + # byebug + # redmine_fixture_path = self.fixture_path + # plugin_fixture_path = Rails.root.join('plugins', 'body_tracking', 'test', 'fixtures') + # all_ft_names = fixture_table_names + + # plugin_ft_names, redmine_ft_names = fixture_table_names.partition do |ft_name| + # File.exists?(plugin_fixture_path.join("#{ft_name.to_s}.yml")) + # end + + # self.fixture_table_names = redmine_ft_names + # fixtures = super + # ActiveSupport::TestCase.fixture_path = plugin_fixture_path + # self.fixture_table_names = plugin_ft_names + # fixtures.merge(super) + + # ActiveSupport::TestCase.fixture_path = redmine_fixture_path + # self.fixture_table_names = all_ft_names + # fixtures + #end end diff --git a/lib/body_tracking/project_patch.rb b/lib/body_tracking/project_patch.rb index d5f82b6..9225fa2 100644 --- a/lib/body_tracking/project_patch.rb +++ b/lib/body_tracking/project_patch.rb @@ -31,7 +31,9 @@ module BodyTracking::ProjectPatch has_many :goals, dependent: :destroy do def binding - find_or_initialize_by(is_binding: true) { |g| g.name = "- binding -" } + find_or_initialize_by(is_binding: true) do |goal| + goal.name = I18n.t('targets.form.binding_goal') + end end end has_many :targets, through: :goals diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb index 6b53570..efb64b6 100644 --- a/test/application_system_test_case.rb +++ b/test/application_system_test_case.rb @@ -21,8 +21,11 @@ class BodyTrackingSystemTestCase < ApplicationSystemTestCase FileUtils.rm Dir.glob(File.join(config.save_path, '*.png')) end - fixtures :projects - plugin_fixtures :quantities, :units, :goals, :exposures, :targets, :quantity_values + fixtures :projects, :users, :members + # Redmine fixtures use explicit IDs, so it's impossible to access them by name. + # Use: 'project_id: 1' and NOT 'project: projects_001' + plugin_fixtures :enabled_modules, :roles, :member_roles, + :quantities, :units, :goals, :exposures, :targets, :quantity_values include AbstractController::Translation diff --git a/test/fixtures/enabled_modules.yml b/test/fixtures/enabled_modules.yml new file mode 100644 index 0000000..311623e --- /dev/null +++ b/test/fixtures/enabled_modules.yml @@ -0,0 +1,3 @@ +enabled_modules_body_tracking: + name: body_tracking + project_id: 1 diff --git a/test/fixtures/goals.yml b/test/fixtures/goals.yml index 5e57cd2..635ae23 100644 --- a/test/fixtures/goals.yml +++ b/test/fixtures/goals.yml @@ -1,4 +1,4 @@ goals_binding: project_id: 1 is_binding: true - name: '- binding -' + name: "<%= I18n.t 'targets.form.binding_goal' %>" diff --git a/test/fixtures/member_roles.yml b/test/fixtures/member_roles.yml new file mode 100644 index 0000000..e58d606 --- /dev/null +++ b/test/fixtures/member_roles.yml @@ -0,0 +1,3 @@ +member_roles_jsmith_manager: + member_id: 1 + role: roles_body_trackers_manager diff --git a/test/fixtures/roles.yml b/test/fixtures/roles.yml new file mode 100644 index 0000000..c4d1400 --- /dev/null +++ b/test/fixtures/roles.yml @@ -0,0 +1,15 @@ +roles_body_trackers_manager: + name: Manage body trackers + builtin: false + issues_visibility: all + users_visibility: all + permissions: | + - :manage_body_trackers + - :view_body_trackers +roles_body_trackers_viewer: + name: View body trackers + builtin: false + issues_visibility: all + users_visibility: all + permissions: | + - :view_body_trackers diff --git a/test/system/targets_test.rb b/test/system/targets_test.rb index 11d11c6..e9c0456 100644 --- a/test/system/targets_test.rb +++ b/test/system/targets_test.rb @@ -6,12 +6,7 @@ class TargetsTest < BodyTrackingSystemTestCase @project1 = projects(:projects_001) - log_user 'alice', 'foo' - end - - def teardown - logout_user - super + log_user 'jsmith', 'jsmith' end def test_index @@ -29,6 +24,31 @@ class TargetsTest < BodyTrackingSystemTestCase assert_selector 'div#targets', visible: :yes, exact_text: t(:label_no_data) end - def test_create_saves_binding_goal_if_nonexistent + def test_index_shows_and_hides_new_target_form + end + + def test_create_binding_target + visit project_targets_path(@project1) + assert_current_path project_targets_path(@project1) + assert_no_selector 'form#new-target-form' + click_link t('targets.contextual.link_new_target') + within 'form#new-target-form' do + assert has_select?(t(:field_goal), selected: t('targets.form.binding_goal')) + assert has_field?(t(:field_effective_from), with: Date.current.strftime) + within 'p.target' do + select quantities(:quantities_energy).name + select '==' + fill_in with: '1750' + select units(:units_kcal).shortname + end + assert_difference 'Target.count' => 1, 'Threshold.count' => 1, 'Goal.count' => 0 do + click_on t(:button_create) + end + end + assert_current_path project_targets_path(@project1) + assert_selector 'table#targets tbody tr', count: @project1.targets.count + end + + def test_create_binding_target_when_binding_goal_does_not_exist end end