Merge remote-tracking branch 'origin/release/7.2' into dev

pull/5642/head
Oliver Günther 7 years ago
commit 79ca840099
No known key found for this signature in database
GPG Key ID: 88872239EB414F99
  1. 1
      app/models/queries/work_packages.rb
  2. 24
      app/models/queries/work_packages/filter/assigned_to_filter.rb
  3. 92
      app/models/queries/work_packages/filter/assignee_or_group_filter.rb
  4. 1
      config/locales/en.yml
  5. 9
      lib/api/v3/queries/filters/query_filter_instance_representer.rb
  6. 41
      lib/api/v3/queries/schemas/assignee_or_group_filter_dependency_representer.rb
  7. 24
      spec/lib/api/v3/queries/filters/query_filter_instance_representer_spec.rb
  8. 8
      spec/models/queries/work_packages/filter/assigned_to_filter_spec.rb
  9. 192
      spec/models/queries/work_packages/filter/assignee_or_group_filter_spec.rb
  10. 3
      spec/support/queries/filters/shared_filter_examples.rb
  11. 4
      spec_legacy/unit/query_spec.rb

@ -33,6 +33,7 @@ module Queries::WorkPackages
register = Queries::Register
register.filter Query, filters_module::AssignedToFilter
register.filter Query, filters_module::AssigneeOrGroupFilter
register.filter Query, filters_module::AuthorFilter
register.filter Query, filters_module::CategoryFilter
register.filter Query, filters_module::CreatedAtFilter

@ -57,28 +57,4 @@ class Queries::WorkPackages::Filter::AssignedToFilter <
def self.key
:assigned_to_id
end
private
def values_replaced
vals = super
vals += group_members_added(vals)
vals + user_groups_added(vals)
end
def group_members_added(vals)
User
.joins(:groups)
.where(groups_users: { id: vals })
.pluck(:id)
.map(&:to_s)
end
def user_groups_added(vals)
Group
.joins(:users)
.where(users_users: { id: vals })
.pluck(:id)
.map(&:to_s)
end
end

@ -0,0 +1,92 @@
#-- encoding: UTF-8
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2017 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-2017 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.
#++
class Queries::WorkPackages::Filter::AssigneeOrGroupFilter <
Queries::WorkPackages::Filter::PrincipalBaseFilter
def allowed_values
@allowed_values ||= begin
values = principal_loader.user_values
if Setting.work_package_group_assignment?
values += principal_loader.group_values
end
me_value + values.sort
end
end
def type
:list_optional
end
def order
4
end
def human_name
I18n.t('query_fields.assignee_or_group')
end
def self.key
:assignee_or_group
end
def where
operator_strategy.sql_for_field(
values_replaced,
self.class.model.table_name,
'assigned_to_id'
)
end
private
def values_replaced
vals = super
vals += group_members_added(vals)
vals + user_groups_added(vals)
end
def group_members_added(vals)
User
.joins(:groups)
.where(groups_users: { id: vals })
.pluck(:id)
.map(&:to_s)
end
def user_groups_added(vals)
Group
.joins(:users)
.where(users_users: { id: vals })
.pluck(:id)
.map(&:to_s)
end
end

@ -1759,6 +1759,7 @@ en:
query_fields:
assigned_to_role: "Assignee's role"
member_of_group: "Assignee's group"
assignee_or_group: "Assignee or belonging group"
subproject_id: "Subproject"
repositories:

@ -78,8 +78,15 @@ module API
next unless represented.ar_object_filter?
represented.value_objects.map do |value_object|
href = begin
api_v3_paths.send(value_object.class.name.demodulize.underscore, value_object.id)
rescue
Rails.logger.error "Failed to get href for value_object #{value_object}"
nil
end
{
href: api_v3_paths.send(value_object.class.name.demodulize.underscore, value_object.id),
href: href,
title: value_object.name
}
end

@ -0,0 +1,41 @@
#-- encoding: UTF-8
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2017 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-2017 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.
#++
module API
module V3
module Queries
module Schemas
class AssigneeOrGroupFilterDependencyRepresenter <
AssignedToFilterDependencyRepresenter
end
end
end
end
end

@ -93,6 +93,30 @@ describe ::API::V3::Queries::Filters::QueryFilterInstanceRepresenter do
.at_path('name')
end
context 'with an invalid value_objects' do
let(:filter) do
Queries::WorkPackages::Filter::AssignedToFilter.new(operator: operator, values: values)
end
let(:values) { ['1'] }
before do
allow(filter)
.to receive(:value_objects)
.and_return([User.anonymous])
end
it "has a 'values' collection" do
expected = {
href: nil,
title: 'Anonymous'
}
is_expected
.to be_json_eql([expected].to_json)
.at_path('_links/values')
end
end
context 'with a non ar object filter' do
let(:values) { ['lorem ipsum'] }
let(:filter) do

@ -102,9 +102,9 @@ describe Queries::WorkPackages::Filter::AssignedToFilter, type: :model do
group.users << assignee
end
it 'returns the work package' do
it 'does not return the work package' do
is_expected
.to match_array [work_package]
.to be_empty
end
end
@ -126,9 +126,9 @@ describe Queries::WorkPackages::Filter::AssignedToFilter, type: :model do
group.users << user
end
it 'returns the work package' do
it 'does not return the work package' do
is_expected
.to match_array [work_package]
.to be_empty
end
end

@ -0,0 +1,192 @@
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2017 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-2017 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.
#++
require 'spec_helper'
describe Queries::WorkPackages::Filter::AssigneeOrGroupFilter, type: :model do
let(:instance) do
filter = described_class.new
filter.values = values
filter.operator = operator
filter
end
let(:operator) { '=' }
let(:values) { [] }
describe 'where filter results' do
let(:work_package) { FactoryGirl.create(:work_package, assigned_to: assignee) }
let(:assignee) { FactoryGirl.create(:user) }
let(:group) { FactoryGirl.create(:group) }
subject { WorkPackage.where(instance.where) }
context 'for the user value' do
let(:values) { [assignee.id.to_s] }
it 'returns the work package' do
is_expected
.to match_array [work_package]
end
end
context 'for the me value with the user being logged in' do
let(:values) { ['me'] }
before do
allow(User)
.to receive(:current)
.and_return(assignee)
end
it 'returns the work package' do
is_expected
.to match_array [work_package]
end
end
context 'for the me value with another user being logged in' do
let(:values) { ['me'] }
before do
allow(User)
.to receive(:current)
.and_return(FactoryGirl.create(:user))
end
it 'does not return the work package' do
is_expected
.to be_empty
end
end
context 'for a group value with the group being assignee' do
let(:assignee) { group }
let(:values) { [group.id.to_s] }
it 'returns the work package' do
is_expected
.to match_array [work_package]
end
end
context 'for a group value with a group member being assignee' do
let(:values) { [group.id.to_s] }
before do
group.users << assignee
end
it 'returns the work package' do
is_expected
.to match_array [work_package]
end
end
context 'for a group value with no group member being assignee' do
let(:values) { [group.id.to_s] }
it 'does not return the work package' do
is_expected
.to be_empty
end
end
context "for a user value with the user's group being assignee" do
let(:values) { [user.id.to_s] }
let(:assignee) { group }
let(:user) { FactoryGirl.create(:user) }
before do
group.users << user
end
it 'returns the work package' do
is_expected
.to match_array [work_package]
end
end
context "for a user value with the user not being member of the assigned group" do
let(:values) { [user.id.to_s] }
let(:assignee) { group }
let(:user) { FactoryGirl.create(:user) }
it 'does not return the work package' do
is_expected
.to be_empty
end
end
context 'for an unmatched value' do
let(:values) { ['0'] }
it 'does not return the work package' do
is_expected
.to be_empty
end
end
end
it_behaves_like 'basic query filter' do
let(:order) { 4 }
let(:type) { :list_optional }
let(:class_key) { :assignee_or_group }
let(:human_name) { I18n.t('query_fields.assignee_or_group') }
describe '#valid_values!' do
let(:user) { FactoryGirl.build_stubbed(:user) }
let(:loader) do
loader = double('loader')
allow(loader)
.to receive(:user_values)
.and_return([[user.name, user.id.to_s]])
allow(loader)
.to receive(:group_values)
.and_return([])
loader
end
before do
allow(::Queries::WorkPackages::Filter::PrincipalLoader)
.to receive(:new)
.and_return(loader)
instance.values = [user.id.to_s, '99999']
end
it 'remove the invalid value' do
instance.valid_values!
expect(instance.values).to match_array [user.id.to_s]
end
end
end
end

@ -49,6 +49,7 @@ shared_examples_for 'basic query filter' do
let(:instance_key) { nil }
let(:class_key) { raise 'needs to be defined' }
let(:type) { raise 'needs to be defined' }
let(:human_name) { nil }
let(:order) { nil }
describe '.key' do
@ -79,7 +80,7 @@ shared_examples_for 'basic query filter' do
describe '#human_name' do
it 'is the l10 name for the filter' do
expect(instance.human_name).to eql(name)
expect(instance.human_name).to eql(human_name.presence || name)
end
end
end

@ -216,10 +216,10 @@ describe Query, type: :model do
query = Query.new(name: '_', filters: [{ assigned_to_id: { operator: '=', values: ['me'] } }])
result = query.results.work_packages
assert_equal WorkPackage.visible.where(assigned_to_id: ([2] + user.reload.group_ids)).sort_by(&:id), result.sort_by(&:id)
assert_equal WorkPackage.visible.where(assigned_to_id: ([2])).sort_by(&:id), result.sort_by(&:id)
assert result.include?(i1)
assert result.include?(i2)
assert !result.include?(i2)
assert !result.include?(i3)
User.current = nil

Loading…
Cancel
Save