# encoding: utf-8 require 'active_record/fixtures' Before do |scenario| unless ScenarioDisabler.empty_if_disabled(scenario) # Reset the DB and load the minimal fixtures for a functional system before each scenario system "RAILS_ENV=test db:test:load >/dev/null 2>/dev/null" ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test']) ActiveRecord::Schema.verbose = false fixtures_path = File.expand_path(Rails.root.join('test/fixtures')) FactoryGirl.create(:admin) unless User.find_by_login("admin") FactoryGirl.create(:anonymous) unless AnonymousUser.count > 0 Setting.notified_events = [] #can not test mailer Dir.glob(File.join(fixtures_path, "*.yml")) do |table_name| ActiveRecord::Fixtures.create_fixtures(fixtures_path, File.basename(table_name).gsub(".yml", "")) end 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 not logged in$/ do User.current = AnonymousUser.first end Given /^(?:|I )am [aA]dmin$/ do steps %Q{ Given I am logged in as \"admin\" } end Given /^I am already logged in as "(.+?)"$/ do |login| # consider using: http://collectiveidea.com/blog/archives/2012/01/05/capybara-cucumber-and-how-the-cookie-crumbles/ # or: https://github.com/railsware/rack_session_access # once the application is rails3 user = User.find_by_login(login) ApplicationController.class_eval do define_method :set_user_session do session[:user_id] = user.id ApplicationController.skip_before_filter :set_user_session ApplicationController.subclasses.each do |subclass_name| unless subclass_name.to_s.include?("Spec::Rails") subclass_name.prepend_before_filter :set_user_session end end end end ApplicationController.prepend_before_filter :set_user_session ApplicationController.subclasses.each do |subclass_name| unless subclass_name.to_s.include?("Spec::Rails") subclass_name.prepend_before_filter :set_user_session end end end def login_user(username, password = "admin") steps %Q{ Given I am on the logout page When I go to the login page And I fill in "#{username}" for "username" And I fill in "#{password}" for "password" And I press "Login" Then I should be logged in as "#{username}" } end Given /^(?:|I )am logged in as "([^\"]*)"$/ do |username| FactoryGirl.create(:admin) unless User.find_by_login("admin") FactoryGirl.create(:anonymous) unless AnonymousUser.count > 0 login_user(username) 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 /^there is 1 [Uu]ser with(?: the following)?:$/ do |table| login = table.rows_hash[:Login].to_s + table.rows_hash[:login].to_s user = User.find_by_login(login) unless login.blank? if user table = table.reject_key(/(L|l)ogin/) else user = FactoryGirl.create(:user) user.password = user.password_confirmation = nil end modify_user(user, table) end Given /^the [Uu]ser "([^\"]*)" has:$/ do |user, table| u = User.find_by_login(user) raise "No such user: #{user}" unless u modify_user(u, table) end Given /^there are the following users:$/ do |table| table.raw.flatten.each do |login| FactoryGirl.create(:user, :login => login) end 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 /^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 [Uu]ser "([^\"]*)" has (\d+) [iI]ssue(?:s)? with(?: the following)?:$/ do |user, count, table| u = User.find_by_login user raise "This user must be member of a project to have issues" unless u.projects.last as_admin count do i = Issue.generate_for_project!(u.projects.last) i.author = u i.assigned_to = u i.tracker = Tracker.find_by_name(table.rows_hash.delete("tracker")) if table.rows_hash["tracker"] send_table_to_object(i, table, {}, method(:add_custom_value_to_issue)) i.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.issue = 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+) [iI]ssue(?:s)? with(?: the following)?:$/ do |project, count, table| p = Project.find_by_name(project) || Project.find_by_identifier(project) as_admin count do i = Issue.generate_for_project!(p) send_table_to_object(i, table, {}, method(:add_custom_value_to_issue)) 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.issue = 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 click on \"→\" When I click on \"Apply\" } end Given /I select to see [cC]olumn(?:s)?$/ do |table| params = "?set_filter=1&" + table.raw.collect(&:first).collect do |name| page.source =~ /