object_helpers.rb 8.73 KB
Newer Older
1
# frozen_string_literal: true
2

3
module ObjectHelpers
4
  def User.generate!(attributes={})
5
    @generated_user_login ||= +'user0'
6 7
    @generated_user_login.succ!
    user = User.new(attributes)
jplang's avatar
jplang committed
8
    user.login = @generated_user_login.dup if user.login.blank?
9 10 11 12 13 14 15 16
    user.mail = "#{@generated_user_login}@example.com" if user.mail.blank?
    user.firstname = "Bob" if user.firstname.blank?
    user.lastname = "Doe" if user.lastname.blank?
    yield user if block_given?
    user.save!
    user
  end

17 18
  def User.add_to_project(user, project, roles=nil)
    roles = Role.find(1) if roles.nil?
19
    roles = [roles] if roles.is_a?(Role)
jplang's avatar
jplang committed
20 21 22
    Member.create!(:principal => user, :project => project, :roles => roles)
  end

23
  def Group.generate!(attributes={})
24
    @generated_group_name ||= +'Group 0'
25 26
    @generated_group_name.succ!
    group = Group.new(attributes)
jplang's avatar
jplang committed
27
    group.name = @generated_group_name.dup if group.name.blank?
28 29 30 31 32 33
    yield group if block_given?
    group.save!
    group
  end

  def Project.generate!(attributes={})
34
    @generated_project_identifier ||= +'project-0000'
35 36
    @generated_project_identifier.succ!
    project = Project.new(attributes)
jplang's avatar
jplang committed
37 38
    project.name = @generated_project_identifier.dup if project.name.blank?
    project.identifier = @generated_project_identifier.dup if project.identifier.blank?
39 40 41 42 43
    yield project if block_given?
    project.save!
    project
  end

44 45 46 47
  def Project.generate_with_parent!(*args)
    attributes = args.last.is_a?(Hash) ? args.pop : {}
    parent = args.size > 0 ? args.first : Project.generate!

48 49 50 51
    project = Project.generate!(attributes) do |p|
      p.parent = parent
    end
    parent.reload if parent
52 53 54
    project
  end

55
  def IssueStatus.generate!(attributes={})
56
    @generated_status_name ||= +'Status 0'
57 58 59 60 61 62 63 64
    @generated_status_name.succ!
    status = IssueStatus.new(attributes)
    status.name = @generated_status_name.dup if status.name.blank?
    yield status if block_given?
    status.save!
    status
  end

65
  def Tracker.generate(attributes={})
66
    @generated_tracker_name ||= +'Tracker 0'
67 68
    @generated_tracker_name.succ!
    tracker = Tracker.new(attributes)
jplang's avatar
jplang committed
69
    tracker.name = @generated_tracker_name.dup if tracker.name.blank?
70
    tracker.default_status ||= IssueStatus.order(:position).first || IssueStatus.generate!
71
    yield tracker if block_given?
72 73 74 75 76
    tracker
  end

  def Tracker.generate!(attributes={}, &block)
    tracker = Tracker.generate(attributes, &block)
77 78 79 80 81
    tracker.save!
    tracker
  end

  def Role.generate!(attributes={})
82
    @generated_role_name ||= +'Role 0'
83 84
    @generated_role_name.succ!
    role = Role.new(attributes)
jplang's avatar
jplang committed
85
    role.name = @generated_role_name.dup if role.name.blank?
86 87 88 89 90
    yield role if block_given?
    role.save!
    role
  end

91
  # Generates an unsaved Issue
92
  # Doesn't send notifications by default, use :notify => true to send them
93
  def Issue.generate(attributes={})
94
    issue = Issue.new(attributes.reverse_merge(:notify => false))
95 96
    issue.project ||= Project.find(1)
    issue.tracker ||= issue.project.trackers.first
97 98 99
    issue.subject = 'Generated' if issue.subject.blank?
    issue.author ||= User.find(2)
    yield issue if block_given?
100 101 102 103
    issue
  end

  # Generates a saved Issue
104
  # Doesn't send notifications by default, use :notify => true to send them
105 106
  def Issue.generate!(attributes={}, &block)
    issue = Issue.generate(attributes, &block)
107 108 109 110
    issue.save!
    issue
  end

111 112 113 114 115 116
  # Generates an issue with 2 children and a grandchild
  def Issue.generate_with_descendants!(attributes={})
    issue = Issue.generate!(attributes)
    child = Issue.generate!(:project => issue.project, :subject => 'Child1', :parent_issue_id => issue.id)
    Issue.generate!(:project => issue.project, :subject => 'Child2', :parent_issue_id => issue.id)
    Issue.generate!(:project => issue.project, :subject => 'Child11', :parent_issue_id => child.id)
117 118 119
    issue.reload
  end

120 121 122 123 124 125
  def Issue.generate_with_child!(attributes={})
    issue = Issue.generate!(attributes)
    Issue.generate!(:parent_issue_id => issue.id)
    issue.reload
  end

jplang's avatar
jplang committed
126 127 128 129 130 131 132 133 134
  def Journal.generate!(attributes={})
    journal = Journal.new(attributes)
    journal.user ||= User.first
    journal.journalized ||= Issue.first
    yield journal if block_given?
    journal.save!
    journal
  end

135
  def Version.generate!(attributes={})
136
    @generated_version_name ||= +'Version 0'
137 138
    @generated_version_name.succ!
    version = Version.new(attributes)
jplang's avatar
jplang committed
139
    version.name = @generated_version_name.dup if version.name.blank?
140
    version.project ||= Project.find(1)
141 142 143 144 145
    yield version if block_given?
    version.save!
    version
  end

146
  def TimeEntry.generate(attributes={})
147 148
    entry = TimeEntry.new(attributes)
    entry.user ||= User.find(2)
149
    entry.author ||= entry.user
150
    entry.issue ||= Issue.find(1) unless entry.project
151 152 153
    entry.project ||= entry.issue.project
    entry.activity ||= TimeEntryActivity.first
    entry.spent_on ||= Date.today
154
    entry.hours ||= 1.0
155 156 157 158 159
    entry
  end

  def TimeEntry.generate!(attributes={}, &block)
    entry = TimeEntry.generate(attributes, &block)
160 161 162 163
    entry.save!
    entry
  end

164
  def AuthSource.generate!(attributes={})
165
    @generated_auth_source_name ||= +'Auth 0'
166 167
    @generated_auth_source_name.succ!
    source = AuthSource.new(attributes)
jplang's avatar
jplang committed
168
    source.name = @generated_auth_source_name.dup if source.name.blank?
169 170 171
    yield source if block_given?
    source.save!
    source
172
  end
jplang's avatar
jplang committed
173 174

  def Board.generate!(attributes={})
175
    @generated_board_name ||= +'Forum 0'
jplang's avatar
jplang committed
176 177
    @generated_board_name.succ!
    board = Board.new(attributes)
jplang's avatar
jplang committed
178 179
    board.name = @generated_board_name.dup if board.name.blank?
    board.description = @generated_board_name.dup if board.description.blank?
jplang's avatar
jplang committed
180 181 182 183
    yield board if block_given?
    board.save!
    board
  end
184 185

  def Attachment.generate!(attributes={})
186
    @generated_filename ||= +'testfile0'
187 188 189 190 191
    @generated_filename.succ!
    attributes = attributes.dup
    attachment = Attachment.new(attributes)
    attachment.container ||= Issue.find(1)
    attachment.author ||= User.find(2)
jplang's avatar
jplang committed
192
    attachment.filename = @generated_filename.dup if attachment.filename.blank?
193 194 195
    attachment.save!
    attachment
  end
196 197

  def CustomField.generate!(attributes={})
198
    @generated_custom_field_name ||= +'Custom field 0'
199 200 201 202 203 204 205 206
    @generated_custom_field_name.succ!
    field = new(attributes)
    field.name = @generated_custom_field_name.dup if field.name.blank?
    field.field_format = 'string' if field.field_format.blank?
    yield field if block_given?
    field.save!
    field
  end
207

208 209 210 211 212 213 214
  def IssueCustomField.generate!(attributes={})
    super do |field|
      field.is_for_all = true unless attributes.key?(:is_for_all)
      field.tracker_ids = Tracker.all.ids unless attributes.key?(:tracker_ids) || attributes.key?(:trackers)
    end
  end

215
  def Changeset.generate!(attributes={})
216
    @generated_changeset_rev ||= +'123456'
217 218 219
    @generated_changeset_rev.succ!
    changeset = new(attributes)
    changeset.repository ||= Project.find(1).repository
jplang's avatar
jplang committed
220
    changeset.revision ||= @generated_changeset_rev.dup
221 222 223 224 225
    changeset.committed_on ||= Time.now
    yield changeset if block_given?
    changeset.save!
    changeset
  end
226 227 228 229 230 231 232 233

  def Query.generate!(attributes={})
    query = new(attributes)
    query.name = "Generated query" if query.name.blank?
    query.user ||= User.find(1)
    query.save!
    query
  end
234

235 236 237 238 239 240 241 242
  def Document.generate!(attributes={})
    document = new(attributes)
    document.title = "Generated document" if document.title.blank?
    document.category ||= DocumentCategory.find(1)
    document.save!
    document
  end

243 244 245 246 247 248 249 250 251 252 253 254 255
  def generate_import(fixture_name='import_issues.csv')
    import = IssueImport.new
    import.user_id = 2
    import.file = uploaded_test_file(fixture_name, 'text/csv')
    import.save!
    import
  end

  def generate_import_with_mapping(fixture_name='import_issues.csv')
    import = generate_import(fixture_name)

    import.settings = {
      'separator' => ";", 'wrapper' => '"', 'encoding' => "UTF-8",
256
      'mapping' => {'project_id' => '1', 'tracker' => '13', 'subject' => '1'}
257 258 259 260
    }
    import.save!
    import
  end
261
end
262

263
module TrackerObjectHelpers
jplang's avatar
jplang committed
264 265
  def generate_transitions!(arg)
    if arg.delete(:clear)
266 267
      WorkflowTransition.where(:tracker_id => id).delete_all
    end
jplang's avatar
jplang committed
268 269 270 271 272 273 274 275 276 277 278
    role_id = arg.delete(:role_id) || 1

    arg.each do |old_status_id, new_status_ids|
      Array.wrap(new_status_ids).each do |new_status_id|
        WorkflowTransition.create!(
          :tracker => self,
          :role_id => role_id,
          :old_status_id => old_status_id,
          :new_status_id => new_status_id
        )
      end
279 280 281 282 283
    end
  end
end
Tracker.send :include, TrackerObjectHelpers

284 285 286 287 288 289 290 291 292 293 294
module IssueObjectHelpers
  def close!
    self.status = IssueStatus.where(:is_closed => true).first
    save!
  end

  def generate_child!(attributes={})
    Issue.generate!(attributes.merge(:parent_issue_id => self.id))
  end
end
Issue.send :include, IssueObjectHelpers