Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,18 @@ class DaysAndHoursForm < ApplicationForm
required: true,
label: TimeEntry.human_attribute_name(:hours),
value: hours_value,
validation_message: hours_validation_message,
data: { "time-entry-target" => "hoursInput",
"action" => "blur->time-entry#hoursChanged keypress.enter->time-entry#hoursKeyEnterPress" }
end

private

# Validations record their errors on :hours, which is only rendered as a hidden input.
def hours_validation_message
model.errors.full_messages_for(:hours).to_sentence.presence
end

def start_time_in_local_time
return if model.start_timestamp.blank?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
<%= component_wrapper do
primer_form_with(**form_options) do |form|
flex_layout(classes: "time-entry-dialog-form-spacing") do |body|
if base_errors.present?
body.with_row do
render(Primer::Alpha::Banner.new(scheme: :danger, icon: :stop)) { base_errors.to_sentence }
end
end
body.with_row(display: show_user ? :block : :none) do
render(TimeEntries::UserForm.new(form, visible: show_user))
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class TimeEntryFormComponent < ApplicationComponent

delegate :project, :work_package, to: :time_entry

def base_errors
time_entry.errors.full_messages_for(:base)
end

def form_options
base = {
model: time_entry,
Expand Down
61 changes: 61 additions & 0 deletions modules/costs/spec/features/time_entry_dialog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,67 @@
end
end

describe "hours limit validation", with_ee: %i[time_entry_time_restrictions] do
let(:permissions) { %i[log_own_time view_own_time_entries view_work_packages] }

before do
visit work_package_path(work_package_a)

find("#action-show-more-dropdown-menu .button").click
find(".menu-item", text: "Log time").click
time_logging_modal.is_visible(true)
end

context "with a maximum number of hours per entry", with_settings: { time_entries_max_hours_per_entry: 2 } do
it "informs the user that the entry exceeds the limit" do
time_logging_modal.update_field("hours_display", "4")

expect do
time_logging_modal.submit
wait_for_network_idle
end.not_to change(TimeEntry, :count)

time_logging_modal.field_has_error("hours_display",
"Hours cannot be more than 2 hours for a single time entry.")
end
end

context "with a maximum number of hours per day", with_settings: { time_entries_max_hours_per_day: 2 } do
it "informs the user that the day total exceeds the limit" do
time_logging_modal.update_field("hours_display", "4")

expect do
time_logging_modal.submit
wait_for_network_idle
end.not_to change(TimeEntry, :count)

time_logging_modal.field_has_error("hours_display",
"Hours cannot exceed 2 hours logged in total for a single day.")
end

it "takes hours already logged on that day into account" do
create(:time_entry, entity: work_package_a, project:, user:, spent_on: Time.zone.today, hours: 1.5)

time_logging_modal.update_field("hours_display", "1")

expect do
time_logging_modal.submit
wait_for_network_idle
end.not_to change(TimeEntry, :count)

time_logging_modal.field_has_error("hours_display",
"Hours cannot exceed 2 hours logged in total for a single day.")

time_logging_modal.update_field("hours_display", "0.5")

expect do
time_logging_modal.submit
wait_for_network_idle
end.to change(TimeEntry, :count).by(1)
end
end
end

describe "when the user can edit time entries" do
let(:permissions) { %i[log_own_time view_own_time_entries edit_own_time_entries view_work_packages] }
let!(:time_entry) { create(:time_entry, entity: work_package_a, project: work_package_a.project, user: user) }
Expand Down
Loading