Skip to content

Commit

Permalink
BirthdayButler: Year fixed for getting birthdaylist at end of year fo…
Browse files Browse the repository at this point in the history
…r next year.
  • Loading branch information
kreinhard committed Jan 9, 2024
1 parent b6092df commit 5b94f96
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class BirthdayButlerService {
month,
locale
)
}_${LocalDateTime.now().year}.docx"
}_${getYear(month)}.docx"
}

/**
Expand Down Expand Up @@ -206,7 +206,7 @@ class BirthdayButlerService {
if (birthdayList.isNotEmpty()) {
val variables = Variables()
variables.put("table", "") // Marker for finding table (should be removed).
variables.put("year", LocalDateTime.now().year)
variables.put("year", getYear(month))
variables.put("month", translateMonth(month, locale = locale))
val birthdayButlerTemplate = configurationService.getOfficeTemplateFile("BirthdayButlerTemplate.docx")
check(birthdayButlerTemplate != null) { "BirthdayButlerTemplate.docx not found" }
Expand Down Expand Up @@ -320,6 +320,25 @@ class BirthdayButlerService {
return StringHelper.format2DigitNumber(number)
}

/**
* Returns the year for the given month. If the month more than 2 months is in the past, the next year is returned.
*/
private fun getYear(month: Month): Int {
return getYear(month, LocalDateTime.now().month)
}

/**
* Only for testing purposes
*/
internal fun getYear(month: Month, currentMonth: Month): Int {
return if (month.ordinal + 2 < currentMonth.ordinal) {
// At the end of the year, the next year is meant.
LocalDateTime.now().year + 1
} else {
return LocalDateTime.now().year
}
}

private val months = arrayOf(
"calendar.month.january",
"calendar.month.february",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/////////////////////////////////////////////////////////////////////////////
//
// Project ProjectForge Community Edition
// www.projectforge.org
//
// Copyright (C) 2001-2024 Micromata GmbH, Germany (www.micromata.com)
//
// ProjectForge is dual-licensed.
//
// This community edition 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; version 3 of the License.
//
// This community edition 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, see http://www.gnu.org/licenses/.
//
/////////////////////////////////////////////////////////////////////////////

package org.projectforge.birthdaybutler

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import java.time.LocalDateTime
import java.time.Month

class BithdayButlerServiceTest {
@Test
fun testGetYear() {
val year = LocalDateTime.now().year
testGetYear(currentMonth = Month.JANUARY, month = Month.JANUARY, year)
testGetYear(currentMonth = Month.JANUARY, month = Month.DECEMBER, year)
testGetYear(currentMonth = Month.FEBRUARY, month = Month.JANUARY, year)
testGetYear(currentMonth = Month.FEBRUARY, month = Month.FEBRUARY, year)
testGetYear(currentMonth = Month.FEBRUARY, month = Month.DECEMBER, year)
testGetYear(currentMonth = Month.JUNE, month = Month.JANUARY, year + 1)
testGetYear(currentMonth = Month.JUNE, month = Month.MARCH, year + 1)
testGetYear(currentMonth = Month.JUNE, month = Month.APRIL, year)
testGetYear(currentMonth = Month.JUNE, month = Month.MAY, year)
testGetYear(currentMonth = Month.JUNE, month = Month.DECEMBER, year)
testGetYear(currentMonth = Month.DECEMBER, month = Month.JANUARY, year + 1)
testGetYear(currentMonth = Month.DECEMBER, month = Month.SEPTEMBER, year + 1)
testGetYear(currentMonth = Month.DECEMBER, month = Month.OCTOBER, year)
testGetYear(currentMonth = Month.DECEMBER, month = Month.NOVEMBER, year)
testGetYear(currentMonth = Month.DECEMBER, month = Month.DECEMBER, year)
}

private fun testGetYear(currentMonth: Month, month: Month, expectedYear: Int) {
Assertions.assertEquals(expectedYear, BirthdayButlerService.getYear(month, currentMonth))
}
}

0 comments on commit 5b94f96

Please sign in to comment.