# encoding: utf-8 #-- copyright # OpenProject is a project management system. # # Copyright (C) 2012-2013 the OpenProject Team # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License version 3. # # See doc/COPYRIGHT.rdoc for more details. #++ require 'active_record/fixtures' require "rack_session_access/capybara" Before do |scenario| unless ScenarioDisabler.empty_if_disabled(scenario) FactoryGirl.create(:admin) unless User.find_by_login("admin") FactoryGirl.create(:anonymous) unless AnonymousUser.count > 0 Setting.notified_events = [] #can not test mailer if Capybara.current_driver.to_s.include?("selenium") Capybara.current_session.driver.browser.manage.window.resize_to(3000, 3000) end end end Given /^I am logged in$/ do @user = FactoryGirl.create :user page.set_rack_session(:user_id => @user.id) end When(/^I log out in the background$/) do page.execute_script("jQuery.ajax('/logout', { success: function () { jQuery(document.body).addClass('logout-ajax') } })") page.should have_selector("body.logout-ajax") end Given /^(?:|I )am not logged in$/ do User.current = AnonymousUser.first end Given /^(?:|I )am [aA]dmin$/ do FactoryGirl.create :admin unless User.where(:login => 'admin').any? FactoryGirl.create :anonymous unless AnonymousUser.count > 0 login('admin', 'adminADMIN!') end Given /^I am already logged in as "(.+?)"$/ do |login| user = User.find_by_login(login) # see https://github.com/railsware/rack_session_access page.set_rack_session(:user_id => user.id) end Given /^(?:|I )am logged in as "([^\"]*)"$/ do |username| FactoryGirl.create :admin unless User.where(:login => 'admin').any? FactoryGirl.create :anonymous unless AnonymousUser.count > 0 login(username, 'adminADMIN!') end Given /^(?:|I )am (not )?impaired$/ do |bool| (user = User.current).impaired = !bool user.save end Given /^there is 1 [pP]roject with(?: the following)?:$/ do |table| p = FactoryGirl.build(:project) send_table_to_object(p, table) end Then /^the project "([^"]*)" is( not)? public$/ do |project_name, negation| p = Project.find_by_name(project_name) p.update_attribute(:is_public, !negation) end Given /^the [Pp]roject "([^\"]*)" has 1 [wW]iki(?: )?[pP]age with the following:$/ do |project, table| p = Project.find_by_name(project) p.wiki.create! unless p.wiki page = FactoryGirl.create(:wiki_page, :wiki => p.wiki) content = FactoryGirl.create(:wiki_content, :page => page) send_table_to_object(page, table) end Given /^the plugin (.+) is loaded$/ do |plugin_name| plugin_name = plugin_name.gsub("\"", "") Redmine::Plugin.all.detect {|x| x.id == plugin_name.to_sym}.present? ? nil : pending("Plugin #{plugin_name} not loaded") end Given /^(?:the )?[pP]roject "([^\"]*)" uses the following [mM]odules:$/ do |project, table| p = Project.find_by_name(project) p.enabled_module_names += table.raw.map { |row| row.first } p.reload end Given /^the [Uu]ser "([^\"]*)" is a "([^\"]*)" (?:in|of) the [Pp]roject "([^\"]*)"$/ do |user, role, project| u = User.find_by_login(user) r = Role.find_by_name(role) p = Project.find_by_name(project) || Project.find_by_identifier(project) as_admin do Member.new.tap do |m| m.user = u m.privacy_unnecessary = true if plugin_loaded?("redmine_dtag_privacy") m.roles << r m.project = p end.save! end end Given /^the [Uu]ser "([^\"]*)" has the following preferences$/ do |user, table| u = User.find_by_login(user) send_table_to_object(u.pref, table) end Given /^there is a(?:n)? (default )?(?:issue)?status with:$/ do |default, table| name = table.raw.select { |ary| ary.include? "name" }.first[table.raw.first.index("name") + 1].to_s IssueStatus.find_by_name(name) || IssueStatus.create(:name => name.to_s, :is_default => !!default) end Given /^there is a(?:n)? (default )?issuepriority with:$/ do |default, table| name = table.raw.select { |ary| ary.include? "name" }.first[table.raw.first.index("name") + 1].to_s project = get_project IssuePriority.new.tap do |prio| prio.name = name prio.is_default = !!default prio.project = project prio.save! end end Given /^there is a [rR]ole "([^\"]*)"$/ do |name| Role.spawn.tap { |r| r.name = name }.save! unless Role.find_by_name(name) end Given /^there are the following roles:$/ do |table| table.raw.flatten.each do |name| FactoryGirl.create(:role, :name => name) unless Role.find_by_name(name) end end Given /^the [rR]ole "([^\"]*)" may have the following [rR]ights:$/ do |role, table| r = Role.find_by_name(role) raise "No such role was defined: #{role}" unless r as_admin do available_perms = Redmine::AccessControl.permissions.collect(&:name) r.permissions = [] table.raw.each do |_perm| perm = _perm.first unless perm.blank? perm = perm.gsub(" ", "_").underscore.to_sym if available_perms.include?(:"#{perm}") r.permissions << perm end end end r.save! end end Given /^the [rR]ole "(.+?)" has no (?:[Pp]ermissions|[Rr]ights)$/ do |role_name| role = Role.find_by_name(role_name) raise "No such role was defined: #{role_name}" unless role as_admin do role.permissions = [] role.save! end end Given /^the [Uu]ser "([^\"]*)" has 1 time [eE]ntry$/ do |user| u = User.find_by_login user p = u.projects.last raise "This user must be member of a project to have issues" unless p i = Issue.generate_for_project!(p) t = TimeEntry.generate t.user = u t.issue = i t.project = p t.activity.project = p t.activity.save! t.save! end Given /^the [Uu]ser "([^\"]*)" has 1 time entry with (\d+\.?\d*) hours? at the project "([^\"]*)"$/ do |user, hours, project| p = Project.find_by_name(project) || Project.find_by_identifier(project) as_admin do t = TimeEntry.generate i = Issue.generate_for_project!(p) t.project = p t.issue = i t.hours = hours.to_f t.user = User.find_by_login user t.activity.project = p t.activity.save! t.save! end end Given /^the [Pp]roject "([^\"]*)" has (\d+) [tT]ime(?: )?[eE]ntr(?:ies|y) with the following:$/ do |project, count, table| p = Project.find_by_name(project) || Project.find_by_identifier(project) as_admin count do t = TimeEntry.generate i = Issue.generate_for_project!(p) t.project = p t.work_package = i t.activity.project = p t.activity.save! send_table_to_object(t, table, :user => Proc.new do |o,v| o.user = User.find_by_login(v) o.save! end, :spent_on => Proc.new do |object, value| # This works for definitions like "2 years ago" number, time_unit, tempus = value.split time = number.to_i.send(time_unit.to_sym).send(tempus.to_sym) object.spent_on = time object.save! end ) end end Given /^the [Pp]roject "([^\"]*)" has (\d+) [Dd]ocument with(?: the following)?:$/ do |project, count, table| p = Project.find_by_name(project) || Project.find_by_identifier(project) as_admin count do d = Document.spawn d.project = p d.category = DocumentCategory.first d.save! send_table_to_object(d, table) end end Given /^the [Pp]roject (.+) has 1 version with(?: the following)?:$/ do |project, table| project.gsub!("\"", "") p = Project.find_by_name(project) || Project.find_by_identifier(project) table.rows_hash["effective_date"] = eval(table.rows_hash["effective_date"]).to_date if table.rows_hash["effective_date"] as_admin do v = Version.generate send_table_to_object(v, table) p.versions << v end end Given /^the [pP]roject "([^\"]*)" has 1 [sS]ubproject$/ do |project| parent = Project.find_by_name(project) p = Project.generate p.set_parent!(parent) p.save! end Given /^the [pP]roject "([^\"]*)" has 1 [sS]ubproject with the following:$/ do |project, table| parent = Project.find_by_name(project) p = FactoryGirl.build(:project) as_admin do send_table_to_object(p, table) end p.set_parent!(parent) p.save! end Given /^there are the following trackers:$/ do |table| table.hashes.each_with_index do |t, i| tracker = Tracker.find_by_name(t['name']) tracker = Tracker.new :name => t['name'] if tracker.nil? tracker.position = t['position'] ? t['position'] : i tracker.is_in_roadmap = t['is_in_roadmap'] ? t['is_in_roadmap'] : true tracker.save! end end Given /^there are the following issue status:$/ do |table| table.hashes.each_with_index do |t, i| status = IssueStatus.find_by_name(t['name']) status = IssueStatus.new :name => t['name'] if status.nil? status.is_closed = t['is_closed'] == 'true' ? true : false status.is_default = t['is_default'] == 'true' ? true : false status.position = t['position'] ? t['position'] : i status.default_done_ratio = t['default_done_ratio'] status.save! end end Given /^the tracker "(.+?)" has the default workflow for the role "(.+?)"$/ do |tracker_name, role_name| role = Role.find_by_name(role_name) tracker = Tracker.find_by_name(tracker_name) tracker.workflows = [] IssueStatus.all(:order => "id ASC").collect(&:id).combination(2).each do |c| tracker.workflows.build(:old_status_id => c[0], :new_status_id => c[1], :role => role) end tracker.save! end Given /^the [iI]ssue "([^\"]*)" has (\d+) [tT]ime(?: )?[eE]ntr(?:ies|y) with the following:$/ do |issue, count, table| i = Issue.find(:last, :conditions => ["subject = '#{issue}'"]) raise "No such issue: #{issue}" unless i as_admin count do t = TimeEntry.generate t.project = i.project t.spent_on = DateTime.now t.work_package = i send_table_to_object(t, table, {:user => Proc.new do |o,v| o.user = User.find_by_login(v) o.save! end}) end end Given /^I select to see [cC]olumn "([^\"]*)"$/ do |column_name| steps %Q{ When I select \"#{column_name}\" from \"available_columns\" When I press \"→\" } end Given /^I select to see [cC]olumn(?:s)?$/ do |table| params = "?set_filter=1&" + table.raw.collect(&:first).collect do |name| page.source =~ /