Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add test case for time logging functionality within the WP view && add some test helper components

Henriette Dingerec3935ba3f79
diff --git a/modules/my_page/spec/features/my/time_entries_current_user_spec.rb b/modules/my_page/spec/features/my/time_entries_current_user_spec.rb
index f63b7e8cb11..4749e2c0e45 100644
--- a/modules/my_page/spec/features/my/time_entries_current_user_spec.rb
+++ b/modules/my_page/spec/features/my/time_entries_current_user_spec.rb
@@ -99,12 +99,8 @@ describe 'My page time entries current user widget spec', type: :feature, js: tr
let(:my_page) do
Pages::My::Page.new
end
- let(:comments_field) { ::EditField.new(page, 'comment') }
- let(:activity_field) { ::EditField.new(page, 'activity') }
- let(:hours_field) { ::EditField.new(page, 'hours') }
- let(:spent_on_field) { ::EditField.new(page, 'spentOn') }
- let(:wp_field) { ::EditField.new(page, 'workPackage') }
let(:cf_field) { ::EditField.new(page, "customField#{custom_field.id}") }
+ let(:time_logging_modal) { ::Components::TimeLoggingModal.new }
before do
login_as user
@@ -170,30 +166,29 @@ describe 'My page time entries current user widget spec', type: :feature, js: tr
find(".fc-content-skeleton td:nth-of-type(5) .te-calendar--add-entry", visible: false).click
end
- expect(page)
- .to have_content(I18n.t('js.time_entry.work_package_required'))
+ time_logging_modal.is_visible true
+
+ time_logging_modal.work_package_is_missing true
- spent_on_field.expect_value((Date.today.beginning_of_week(:sunday) + 3.days).strftime)
+ time_logging_modal.has_field_with_value 'spentOn', (Date.today.beginning_of_week(:sunday) + 3.days).strftime
expect(page)
.not_to have_selector('.ng-spinner-loader')
- wp_field.input_element.click
- wp_field.set_value(other_work_package.subject)
+ time_logging_modal.update_work_package_field other_work_package.subject
- expect(page)
- .to have_no_content(I18n.t('js.time_entry.work_package_required'))
+ time_logging_modal.work_package_is_missing false
- comments_field.set_value('Comment for new entry')
+ time_logging_modal.update_field 'comment', 'Comment for new entry'
- activity_field.input_element.click
- activity_field.set_value(activity.name)
+ time_logging_modal.update_field 'activity', activity.name
- hours_field.set_value('4')
+ time_logging_modal.update_field 'hours', 4
sleep(0.1)
- click_button I18n.t('js.label_create')
+ time_logging_modal.perform_action 'Create'
+ time_logging_modal.is_visible false
my_page.expect_and_dismiss_notification message: I18n.t(:notice_successful_create)
@@ -215,27 +210,22 @@ describe 'My page time entries current user widget spec', type: :feature, js: tr
find(".fc-content-skeleton td:nth-of-type(3) .fc-event-container .te-calendar--time-entry").click
end
- expect(page)
- .to have_content(I18n.t('js.time_entry.label'))
+ time_logging_modal.is_visible true
- activity_field.input_element.click
- activity_field.set_value(other_activity.name)
+ time_logging_modal.update_field 'activity', other_activity.name
- wp_field.input_element.click
# As the other_work_package now has time logged, it is now considered to be a
# recent work package.
- within('.ng-dropdown-header') do
- click_link(I18n.t('js.label_recent'))
- end
- wp_field.set_value(other_work_package.subject)
+ time_logging_modal.update_work_package_field other_work_package.subject, true
- hours_field.set_value('6')
+ time_logging_modal.update_field 'hours', 6
- comments_field.set_value('Some comment')
+ time_logging_modal.update_field 'comment', 'Some comment'
cf_field.set_value('Cf text value')
- click_button 'Save'
+ time_logging_modal.perform_action 'Save'
+ time_logging_modal.is_visible false
sleep(0.1)
my_page.expect_and_dismiss_notification message: I18n.t(:notice_successful_update)
@@ -265,8 +255,11 @@ describe 'My page time entries current user widget spec', type: :feature, js: tr
find(".fc-content-skeleton td:nth-of-type(6) .fc-event-container .te-calendar--time-entry").click
end
- click_button 'Delete'
+ time_logging_modal.is_visible true
+ time_logging_modal.perform_action 'Delete'
+
page.driver.browser.switch_to.alert.accept
+ time_logging_modal.is_visible false
within entries_area.area do
expect(page)
diff --git a/spec/features/support/components/time_logging_modal.rb b/spec/features/support/components/time_logging_modal.rb
new file mode 100644
index 00000000000..a82bb7b15a3
--- /dev/null
+++ b/spec/features/support/components/time_logging_modal.rb
@@ -0,0 +1,143 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Components
+ class TimeLoggingModal
+ include Capybara::DSL
+ include RSpec::Matchers
+
+ attr_reader :activity_field,
+ :comment_field,
+ :hours_field,
+ :spent_on_field,
+ :work_package_field
+
+ def initialize
+ @activity_field = EditField.new(page, 'activity')
+ @comment_field = EditField.new(page, 'comment')
+ @hours_field = EditField.new(page, 'hours')
+ @spent_on_field = EditField.new(page, 'spentOn')
+ @work_package_field = EditField.new(page, 'workPackage')
+ end
+
+ def is_visible(visible)
+ if visible
+ within modal_container do
+ expect(page)
+ .to have_content(I18n.t('js.button_log_time'))
+ end
+ else
+ expect(page).to have_no_selector '.op-modal--modal-container'
+ end
+ end
+
+ def has_field_with_value(field, value)
+ within modal_container do
+ expect(page).to have_field field_identifier(field), with: value
+ end
+ end
+
+ def shows_field(field, visible)
+ within modal_container do
+ if visible
+ expect(page).to have_selector "##{field_identifier(field)}"
+ else
+ expect(page).to have_no_selector "##{field_identifier(field)}"
+ end
+ end
+ end
+
+ def update_field(field_name, value)
+ field = field_object field_name
+ field.input_element.click
+ field.set_value value
+ end
+
+ def update_work_package_field(value, recent = false)
+ work_package_field.input_element.click
+
+ if recent
+ within('.ng-dropdown-header') do
+ click_link(I18n.t('js.label_recent'))
+ end
+ end
+
+ work_package_field.set_value(value)
+ end
+
+ def perform_action(action)
+ within modal_container do
+ click_button action
+ end
+ end
+
+ def work_package_is_missing(missing)
+ if missing
+ expect(page)
+ .to have_content(I18n.t('js.time_entry.work_package_required'))
+ else
+ expect(page)
+ .to have_no_content(I18n.t('js.time_entry.work_package_required'))
+ end
+ end
+
+ private
+
+ def field_identifier(field_name)
+ case field_name
+ when 'spent_on'
+ 'wp-new-inline-edit--field-spentOn'
+ when 'work_package'
+ 'wp-new-inline-edit--field-workPackage'
+ else
+ nil
+ end
+ end
+
+ def field_object(field_name)
+ case field_name
+ when 'activity'
+ activity_field
+ when 'hours'
+ hours_field
+ when 'spent_on'
+ spent_on_field
+ when 'comment'
+ comment_field
+ when 'work_package'
+ work_package_field
+ else
+ nil
+ end
+ end
+
+ def modal_container
+ page.find('.op-modal--modal-container')
+ end
+ end
+end
diff --git a/spec/features/work_packages/spent_time_display_spec.rb b/spec/features/work_packages/spent_time_display_spec.rb
new file mode 100644
index 00000000000..386d8d850cc
--- /dev/null
+++ b/spec/features/work_packages/spent_time_display_spec.rb
@@ -0,0 +1,89 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2020 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+
+describe 'Logging time within the work package view', type: :feature, js: true do
+ let(:project) { FactoryBot.create :project }
+ let(:admin) { FactoryBot.create :admin }
+ let(:user_without_permissions) do
+ FactoryBot.create(:user,
+ member_in_project: project,
+ member_with_permissions: %i[view_time_entries view_work_packages edit_work_packages])
+ end
+
+ let!(:activity) { FactoryBot.create :time_entry_activity, project: project }
+ let(:spent_time_field) { ::SpentTimeEditField.new(page, 'spentTime') }
+ let(:time_logging_modal) { ::Components::TimeLoggingModal.new }
+
+ let(:work_package) { FactoryBot.create :work_package, project: project }
+ let(:wp_page) { Pages::FullWorkPackage.new(work_package, project) }
+
+
+ context 'as an admin' do
+ before do
+ login_as(admin)
+ wp_page.visit!
+ loading_indicator_saveguard
+ end
+
+ it 'shows a logging button within the display field and can log time via a modal' do
+ spent_time_field.timeLogIconVisible true
+
+ # click on button opens modal
+ spent_time_field.openTimeLogModal
+ time_logging_modal.is_visible true
+
+ # the fields are visible
+ time_logging_modal.has_field_with_value 'spent_on', Date.today.strftime("%Y-%m-%d")
+ time_logging_modal.shows_field 'work_package', false
+
+ time_logging_modal.update_field 'activity', activity.name
+
+ # a click on save creates a time entry
+ time_logging_modal.perform_action 'Create'
+ wp_page.expect_and_dismiss_notification message: 'Successful creation.'
+
+ # the value is updated automatically
+ spent_time_field.expect_display_value '1 h'
+ end
+ end
+
+ context 'as a user who cannot log time' do
+ before do
+ login_as(user_without_permissions)
+ wp_page.visit!
+ loading_indicator_saveguard
+ end
+
+ it 'shows no logging button within the display field' do
+ spent_time_field.timeLogIconVisible false
+ spent_time_field.expect_display_value '-'
+ end
+ end
+end
diff --git a/spec/support/edit_fields/spent_time_edit_field.rb b/spec/support/edit_fields/spent_time_edit_field.rb
new file mode 100644
index 00000000000..7d4787a552e
--- /dev/null
+++ b/spec/support/edit_fields/spent_time_edit_field.rb
@@ -0,0 +1,21 @@
+require_relative './edit_field'
+
+class SpentTimeEditField < EditField
+ def timeLogIconVisible(visible)
+ if visible
+ expect(page).to have_selector("#{@selector} #{display_selector} #{icon}")
+ else
+ expect(page).to have_no_selector("#{@selector} #{display_selector} #{icon}")
+ end
+ end
+
+ def openTimeLogModal
+ page.find("#{@selector} #{display_selector} #{icon}").click
+ end
+
+ private
+
+ def icon
+ '.icon-time'
+ end
+end