#The MIT License (MIT) # #Copyright (c) 2026 rick barrette # #Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: # #The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. # #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. require File.expand_path('../test_helper', __dir__) class EstimateIssueBuilderTest < ActiveSupport::TestCase include QboFakeServiceHelper fixtures :projects, :users, :email_addresses, :roles, :members, :member_roles, :issues, :issue_statuses, :trackers, :projects_trackers, :enabled_modules, :enumerations, :workflows, :custom_fields, :custom_values, :custom_fields_projects, :custom_fields_trackers, :issue_relations Builder = RedmineQboBilling::EstimateIssueBuilder def setup Setting.stubs(:plugin_redmine_qbo).returns('issue_default_tracker_id' => '2', 'issue_default_project_id' => '1') QboEstimateLineIssue.delete_all Estimate.where(id: 598).delete_all local_customer @estimate = Estimate.create!(id: 598, doc_number: '1068', customer_id: 9101) @estimate.stubs(:line_items).returns([ qbo_sales_line(1, 10, 'Labor', 2, 120, description: 'Install lift kit'), qbo_sales_line(2, 11, 'Brake pads', 4, 30, description: 'Pads'), qbo_sales_line(3, 10, 'Labor', 0, 120, description: 'Diagnose noise'), qbo_sales_line(4, 10, 'Labor', 1, 120, description: ' ') ]) @project = Project.find(1) @author = User.find(1) User.current = @author end def rows(*line_ids, **extra) line_ids.to_h { |id| [id.to_s, { 'selected' => '1', 'tracker_id' => '1' }.merge(extra.transform_keys(&:to_s))] } end def test_settings assert_equal 2, Builder.default_tracker_id assert_equal 1, Builder.default_project_id end def test_labor_hours assert_equal 3.0, Builder.labor_hours(@estimate) assert_equal 0.0, Builder.labor_hours(nil) end def test_proposals_are_labor_lines_with_a_description proposals = Builder.new(@estimate).proposals assert_equal %w[1 3], proposals.map(&:line_id) assert_equal ['Install lift kit', 2.0], [proposals.first.description, proposals.first.hours] assert_nil proposals.last.hours assert_not proposals.first.created? assert_equal [], Builder.new(nil).proposals end def test_create_issues builder = Builder.new(@estimate, defaults: { start_date: Date.new(2026, 11, 23), assignee: User.find(3) }) created = builder.create_issues(project: @project, rows: rows(1, 3, estimated_hours: '1.5'), author: @author) assert_empty builder.errors # Subjects are title-cased by the issue patch assert_equal ['Install Lift Kit', 'Diagnose Noise'], created.map(&:subject) issue = created.first.reload assert_equal [598, 9101, 1.5, Date.new(2026, 11, 23)], [issue.estimate_id, issue.customer_id, issue.estimated_hours, issue.start_date] assert_match 'Estimate #1068', issue.description assert_equal 2, QboEstimateLineIssue.where(estimate_id: 598).count assert issue.relations.any? { |r| r.other_issue(issue) == created.last } # Shown as created; can't be made again again = Builder.new(@estimate) assert again.proposals.all?(&:created?) assert_equal [], again.create_issues(project: @project, rows: rows(1), author: @author) assert_equal [I18n.t(:error_no_estimate_lines_selected)], again.errors end def test_later_batches_are_related_to_earlier_ones first = Builder.new(@estimate).create_issues(project: @project, rows: rows(1), author: @author) second = Builder.new(@estimate).create_issues(project: @project, rows: rows(3), author: @author) assert IssueRelation.where(issue_from_id: [first.first.id, second.first.id], issue_to_id: [first.first.id, second.first.id]).exists? end def test_nothing_is_created_when_one_issue_fails builder = Builder.new(@estimate) count = Issue.count created = builder.create_issues(project: @project, rows: rows(1).merge(rows(3, tracker_id: '999')), author: @author) assert_equal [], created assert_equal count, Issue.count assert_match 'Diagnose noise', builder.errors.first assert_equal 0, QboEstimateLineIssue.count end def test_subject_and_default_description_overrides builder = Builder.new(@estimate, defaults: { description: 'From the appointment' }) created = builder.create_issues(project: @project, rows: rows(1, subject: 'Custom subject'), author: @author) assert_equal 'Custom Subject', created.first.subject assert_equal 'From the appointment', created.first.description end def test_unassignable_user_is_left_off outsider = User.find(7) created = Builder.new(@estimate, defaults: { assignee: outsider }).create_issues(project: @project, rows: rows(1), author: @author) assert_nil created.first.assigned_to end def test_subtasks_take_the_parents_customer parent = Issue.find(1) parent.update_column(:customer_id, 9102) created = Builder.new(@estimate, parent: parent).create_issues(project: @project, rows: rows(1), author: @author) issue = created.first.reload assert_equal parent.id, issue.parent_id assert_equal 9102, issue.customer_id # Not related to its own parent assert_not issue.relations.any? { |r| r.other_issue(issue) == parent } end def test_plugins_set_their_fields Redmine::Hook.expects(:call_hook).with(:qbo_billing_build_issue, has_key(:issue)).once Builder.new(@estimate).create_issues(project: @project, rows: rows(1), author: @author) end def test_relate_all_skips_existing_relations issues = Issue.where(id: [1, 2, 3]).order(:id).to_a IssueRelation.where(issue_from_id: [1, 2, 3]).or(IssueRelation.where(issue_to_id: [1, 2, 3])).delete_all IssueRelation.create!(issue_from: issues[0], issue_to: issues[1], relation_type: 'relates') assert_equal 2, Builder.relate_all(issues + [nil, issues[0]]) assert_equal 0, Builder.relate_all(issues) assert_equal 0, Builder.relate_all([issues.first]) end end