Skip to content

Commit

Permalink
Maintenance: Ported test/integration/object_manager_test.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaefer committed Mar 1, 2023
1 parent d873b2a commit b3c45f5
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 1,212 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@ bundle exec rspec --exclude-pattern "spec/system/**/*_spec.rb" -t ~searchindex -
echo "Running basic minitest tests..."
bundle exec rake zammad:db:reset
bundle exec rake test:units
ruby -I test/ test/integration/object_manager_test.rb
1 change: 0 additions & 1 deletion .gitlab/ci/test/unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
script:
- !reference [.scripts, zammad_db_init]
- bundle exec rake test:units
- bundle exec rails test test/integration/object_manager_test.rb

unit:mysql:
stage: test
Expand Down
1 change: 0 additions & 1 deletion .rubocop/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ Lint/BooleanSymbol:
Exclude:
- "db/seeds/object_manager_attributes.rb"
- "spec/requests/integration/object_manager_attributes_spec.rb"
- "test/integration/object_manager_test.rb"

Lint/InterpolationCheck:
Description: 'Raise warning for interpolation in single q strs'
Expand Down
66 changes: 66 additions & 0 deletions spec/models/object_manager/attribute/object/ticket_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe 'ObjectManager::Attribute::Object::Ticket', aggregate_failures: true, db_strategy: :reset do
shared_context 'with ticket attribute setup' do
before { attribute }

let(:attribute) do
attribute = create(:object_manager_attribute_text)
ObjectManager::Attribute.migration_execute

attribute
end
let(:ticket) { create(:ticket) }
end

describe 'add ticket attribute' do
include_context 'with ticket attribute setup'

it 'is successful' do
ticket.update(attribute.name => 'Bazinga!')
expect(ticket.reload).to have_attributes(attribute.name => 'Bazinga!')
end
end

describe 'update ticket attribute' do
include_context 'with ticket attribute setup'

it 'is successful' do
skip 'Missing error handling on edit misconfiguration.'

ticket.update!(attribute.name => 'Bazinga!')

attributes = attribute.attributes
attributes.delete('data_option_new')
attributes['data_option'] = {
maxlength: 3,
type: 'text',
null: false,
}
ObjectManager::Attribute.add(attributes.deep_symbolize_keys)
ObjectManager::Attribute.migration_execute
expect { ticket.reload }.not_to raise_error

new_ticket = create(:ticket).tap { |t| t.update!(attribute.name => 'Bazinga!') }
expect(new_ticket.attributes[attribute.name].length).to be(3)
end
end

describe 'remove ticket attribute' do
include_context 'with ticket attribute setup'

it 'is successful' do
ticket.update!(attribute.name => 'Bazinga!')

attribute_name = attribute.name
ObjectManager::Attribute.remove(
object: 'Ticket',
name: attribute_name
)
ObjectManager::Attribute.migration_execute
expect(ticket.reload.attributes).not_to include(attribute_name)
end
end
end
32 changes: 32 additions & 0 deletions spec/models/object_manager/attribute/validation/data_types_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe 'ObjectManager::Attribute::Validation::DataTypes', aggregate_failures: true do

%w[
input
user_autocompletion
checkbox
select
multiselect
tree_select
multi_tree_select
datetime
date
tag
richtext
textarea
integer
autocompletion_ajax
autocompletion_ajax_customer_organization
boolean
user_permission
active
].freeze.each do |data_type|
it "validates #{data_type} data type" do
expect(ObjectManager::Attribute::DATA_TYPES).to include(data_type)
end
end

end
109 changes: 109 additions & 0 deletions spec/models/object_manager/attribute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@
end

describe 'Class methods:' do
describe '.pending_migration?', db_strategy: :reset do
it 'returns false if there are no pending migrations' do
expect(described_class.pending_migration?).to be false
end

it 'returns true if there are pending migrations' do
create(:object_manager_attribute_text)
expect(described_class.pending_migration?).to be true
end

it 'returns false if migration was executed' do
create(:object_manager_attribute_text)
described_class.migration_execute
expect(described_class.pending_migration?).to be false
end
end

describe '.attribute_to_references_hash_objects' do
it 'returns classes with conditions' do
expect(described_class.attribute_to_references_hash_objects).to match_array [Trigger, Overview, Job, Sla, Report::Profile ]
Expand Down Expand Up @@ -474,4 +491,96 @@
expect(select_field.reload.data_option[:historical_options]).to eq(expect_result)
end
end

describe '#add' do
context 'when data is valid' do
let(:attribute) do
{
object: 'Ticket',
name: 'test1',
display: 'Test 1',
data_type: 'input',
data_option: {
maxlength: 200,
type: 'text',
null: false,
},
active: true,
screens: {},
position: 20,
created_by_id: 1,
updated_by_id: 1,
editable: false,
to_migrate: false,
}
end

it 'is successful' do
expect { described_class.add(attribute) }.to change(described_class, :count)
expect(described_class.get(object: 'Ticket', name: 'test1')).to have_attributes(attribute)
end
end

context 'when data is invalid' do
let(:attribute) do
{
object: 'Ticket',
name: 'test2_id',
display: 'Test 2 with id',
data_type: 'input',
data_option: {
maxlength: 200,
type: 'text',
null: false,
},
active: true,
screens: {},
position: 20,
created_by_id: 1,
updated_by_id: 1,
}
end

it 'raises an error' do
expect { described_class.add(attribute) }.to raise_error(ActiveRecord::RecordInvalid)
end
end
end

describe '#get' do
context 'when attribute exists' do
before do
create(:object_manager_attribute_text, name: 'test3')
end

it 'returns the attribute' do
expect(described_class.get(object: 'Ticket', name: 'test3')).to have_attributes(name: 'test3', editable: true)
end
end

context 'when attribute does not exist' do
it 'returns nil' do
expect(described_class.get(object: 'Ticket', name: 'test4')).to be_nil
end
end
end

describe '#remove' do
context 'when attribute exists' do
before do
create(:object_manager_attribute_text, name: 'test3')
end

it 'is successful' do
expect { described_class.remove(object: 'Ticket', name: 'test3') }.to change(described_class, :count)
expect(described_class.get(object: 'Ticket', name: 'test3')).to be_nil
end
end

context 'when attribute does not exist' do
it 'raises an error' do
expect { described_class.remove(object: 'Ticket', name: 'test4') }.to raise_error(RuntimeError)
end
end
end
end
21 changes: 21 additions & 0 deletions spec/models/object_manager_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'
RSpec.describe ObjectManager, aggregate_failures: true, type: :model do

describe 'class methods' do
describe 'list_objects' do
it 'returns an array of objects' do
expect(described_class.list_objects).to be_an(Array)
expect(described_class.list_objects.sort).to match_array(%w[Group Organization User Ticket TicketArticle])
end
end

describe 'list_frontend_objects' do
it 'returns an array of objects' do
expect(described_class.list_frontend_objects).to be_an(Array)
expect(described_class.list_frontend_objects.sort).to match_array(%w[Group Organization User Ticket])
end
end
end
end
51 changes: 51 additions & 0 deletions spec/models/ticket/selector/attribute_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe 'Ticket::Selector', db_strategy: :reset, searchindex: true do
before do
Ticket.destroy_all
attribute
tickets
searchindex_model_reload([Ticket])
end

let(:agent) { create(:agent, groups: [Group.first]) }
let(:attribute) do
attribute = create(:object_manager_attribute_text)
ObjectManager::Attribute.migration_execute

attribute
end
let(:tickets) do
tickets = create_list(:ticket, 10, group: Group.first)
tickets.each_with_index do |ticket, index|
ticket[attribute.name] = index.odd? ? '1st value' : '2nd value'
ticket.save!
end
end
let(:condition) do
{
"ticket.#{attribute.name}" => {
operator: 'is',
value: '1st value',
},
}
end

describe 'select by condition attribute' do
context 'when using the ticket selector' do
it 'is successful' do
ticket_count, = Ticket.selectors(condition, limit: 100)
expect(ticket_count).to eq(5)
end
end

context 'when using the search index backend' do
it 'is successful' do
result = SearchIndexBackend.selectors('Ticket', condition, { current_user: agent })
expect(result[:count]).to eq(5)
end
end
end
end
66 changes: 66 additions & 0 deletions spec/system/overview/attribute_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe 'Overview with custom attributes', authenticated_as: :authenticate, db_strategy: :reset, type: :system do
let(:attribute) { create(:object_manager_attribute_boolean) }
let(:agent) { create(:agent, groups: [Group.find_by(name: 'Users')]) }
let(:overview) { nil }
let(:ticket) { nil }

def authenticate
agent
attribute
ObjectManager::Attribute.migration_execute
overview
ticket

true
end

before do
visit "ticket/view/#{overview.link}"
end

context 'when the custom attribute used in a view in an overview' do
let(:overview) do
create(:overview,
view: { s: ['title', 'number', attribute.name],
view_mode_default: 's' })
end

it 'shows the custom attribute display description' do
within :active_content do
expect(page).to have_text attribute.display.to_s.upcase
end
end
end

context 'when the custom attribute is used as condition in an overview' do
let(:overview) do
create(:overview,
condition: { "ticket.#{attribute.name}" => { operator: 'is', value: true } },
view: { s: ['title', 'number', attribute.name],
view_mode_default: 's' })
end

context 'with no ticket with custom attribute value true' do
it 'shows no entries' do
within :active_content do
expect(page).to have_text 'NO ENTRIES'
end
end
end

context 'with a ticket with custom attribute value true' do
let(:ticket) { create(:ticket, group: Group.find_by(name: 'Users'), attribute.name => true) }

it 'shows the ticket' do
within :active_content do
expect(page).to have_text attribute.display.to_s.upcase
expect(page).to have_text ticket.title
end
end
end
end
end
Loading

0 comments on commit b3c45f5

Please sign in to comment.