diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml new file mode 100644 index 0000000..65c17bf --- /dev/null +++ b/.github/workflows/integration.yml @@ -0,0 +1,111 @@ +# Integration tests (tests/Integration) inside a real Nextcloud server, on +# every database Nextcloud supports for apps: SQLite, MySQL, MariaDB and +# PostgreSQL, for the oldest and the newest supported Nextcloud version. +# +# Each job checks out nextcloud/server, puts this app into its apps/ folder, +# installs Nextcloud on the job's database and runs the tests with the +# server's own PHPUnit version. +name: Integration tests + +on: + pull_request: + push: + branches: [master] + +permissions: + contents: read + +concurrency: + group: integration-${{ github.head_ref || github.run_id }} + cancel-in-progress: true + +jobs: + integration: + runs-on: ubuntu-latest + timeout-minutes: 20 + strategy: + fail-fast: false + matrix: + server: ['stable31', 'stable35'] + database: + - name: SQLite + type: sqlite + image: '' + port: 3306 + options: '' + - name: MySQL 8.4 + type: mysql + image: mysql:8.4 + port: 3306 + options: --health-cmd="mysqladmin ping" --health-interval 5s --health-timeout 2s --health-retries 10 + - name: MariaDB 11.4 + type: mysql + image: mariadb:11.4 + port: 3306 + options: --health-cmd="mariadb-admin ping" --health-interval 5s --health-timeout 2s --health-retries 10 + - name: PostgreSQL 16 + type: pgsql + image: postgres:16 + port: 5432 + options: --health-cmd pg_isready --health-interval 5s --health-timeout 2s --health-retries 10 + + name: ${{ matrix.database.name }}, Nextcloud ${{ matrix.server }} + + services: + # Not started for SQLite (empty image) + database: + image: ${{ matrix.database.image }} + ports: + - 4444:${{ matrix.database.port }}/tcp + env: + MYSQL_ROOT_PASSWORD: rootpassword + POSTGRES_USER: root + POSTGRES_PASSWORD: rootpassword + # The installer connects to this database before it would create it + POSTGRES_DB: nextcloud + options: ${{ matrix.database.options }} + + steps: + - name: Checkout Nextcloud server + uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + with: + persist-credentials: false + repository: nextcloud/server + ref: ${{ matrix.server }} + submodules: true + path: server + + - name: Checkout app + uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + with: + persist-credentials: false + path: server/apps/timetracker + + - name: Set up php + uses: shivammathur/setup-php@f3e473d116dcccaddc5834248c87452386958240 # 2.37.2 + with: + php-version: '8.3' + extensions: bz2, ctype, curl, dom, fileinfo, gd, iconv, intl, json, libxml, mbstring, openssl, pcntl, posix, session, simplexml, xmlreader, xmlwriter, zip, zlib, sqlite, pdo_sqlite, mysql, pdo_mysql, pgsql, pdo_pgsql + coverage: none + ini-file: development + ini-values: disable_functions="" + + - name: Install the server's PHPUnit + run: composer install --no-progress --working-dir=server/vendor-bin/phpunit + + - name: Use strict GROUP BY rules (MySQL, MariaDB) + if: matrix.database.type == 'mysql' + run: echo "SET GLOBAL sql_mode=(SELECT CONCAT(@@sql_mode,',ONLY_FULL_GROUP_BY'));" | mysql -h 127.0.0.1 -P 4444 -u root -prootpassword + + - name: Install Nextcloud and enable the app + working-directory: server + run: | + mkdir data + php occ maintenance:install --verbose --database=${{ matrix.database.type }} --database-name=nextcloud \ + --database-host=127.0.0.1 --database-port=4444 --database-user=root --database-pass=rootpassword \ + --admin-user admin --admin-pass admin + php occ app:enable timetracker + + - name: Run integration tests + working-directory: server/apps/timetracker + run: ../../vendor-bin/phpunit/vendor/bin/phpunit -c phpunit.integration.xml diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index c894555..86cef99 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -1,7 +1,7 @@ # Unit tests only, against the nextcloud/ocp stub package — no live # Nextcloud server or database is booted, which keeps this job fast and free -# of external services. tests/Integration (real server + DB) is intentionally -# not run here; see the linked GitHub issue for the multi-database follow-up. +# of external services. tests/Integration (real server + DB) runs in +# integration.yml. name: PHPUnit on: diff --git a/lib/Controller/AjaxController.php b/lib/Controller/AjaxController.php index 29b9d76..10143a2 100644 --- a/lib/Controller/AjaxController.php +++ b/lib/Controller/AjaxController.php @@ -1250,18 +1250,30 @@ public function deleteGoal($id) { return $this->getGoals(); } + /** + * The user's time zone. Goal periods use it, like the report grouping in + * ReportItemMapper does. + */ + private function userTimeZone(): \DateTimeZone { + return \OCP\Server::get(\OCA\TimeTracker\Service\UserTimeZone::class)->get($this->userId); + } + + /** + * Monday 00:00 of the ISO week containing $timestamp, in the user's time zone. + */ public function getStartOfWeek($timestamp){ - $date = new \DateTime('@'.$timestamp); - $weeknumber = $date->format("W"); - $year = $date->format("Y"); - $weekstartdt = new \DateTime(); - $weekstartdt->setTime(0, 0, 0, 0); - $weekstartdt->setISODate($year, $weeknumber); - - return $weekstartdt; + $date = (new \DateTime('@'.$timestamp))->setTimezone($this->userTimeZone()); + // "o" is the ISO year that week "W" belongs to: 30 Dec 2024 is in week 1 of 2025 + $date->setISODate((int)$date->format('o'), (int)$date->format('W')); + $date->setTime(0, 0, 0, 0); + + return $date; } + /** + * The 1st of the month containing $timestamp, 00:00 in the user's time zone. + */ public function getStartOfMonth($timestamp){ - $date = new \DateTime('@'.$timestamp); + $date = (new \DateTime('@'.$timestamp))->setTimezone($this->userTimeZone()); $date->modify('first day of this month'); $date->setTime(0, 0, 0, 0); diff --git a/lib/Db/ReportItemMapper.php b/lib/Db/ReportItemMapper.php index 6986ecd..d98e30c 100644 --- a/lib/Db/ReportItemMapper.php +++ b/lib/Db/ReportItemMapper.php @@ -1,263 +1,221 @@ dbengine = 'MYSQL'; - $provider = $db->getDatabaseProvider(); - if ($provider === IDBConnection::PLATFORM_POSTGRES) { - $this->dbengine = 'POSTGRES'; - } else if ($provider === IDBConnection::PLATFORM_SQLITE) { - $this->dbengine = 'SQLITE'; - } - parent::__construct($db, 'timetracker_work_interval'); - } - -/* - public $id; - public $name; - public $projectId; - public $userUid; - public $start; - public $totalDuration; - public $project; - public $clientId; - public $client; -*/ - + private const GROUPABLE = ['name', 'project', 'client', 'userUid']; - public function report($user, $from, $to, $filterProjectId, $filterClientId, $filterTagId, $timegroup, $groupOn1, $groupOn2, $admin, $start, $limit ){ + private $userTimeZone; - $selectFields = ['min(wi.id) as id', 'sum(duration) as "totalDuration"']; + public function __construct(IDBConnection $db, UserTimeZone $userTimeZone) { + parent::__construct($db, 'timetracker_work_interval'); + $this->userTimeZone = $userTimeZone; + } - if ($timegroup !== null) { - $selectFields[]= "SUM(wi.cost) as cost"; + /** + * @param string $user only report this user's entries, unless $admin + * @param int|string|null $from only entries starting after this unix timestamp + * @param int|string|null $to only entries starting before this unix timestamp + * @param array $filterProjectId project ids; a null element also matches entries without project + * @param array|string $filterClientId client ids; a null element also matches entries without client + * @param mixed $filterTagId unused + * @param string|null $timegroup '', 'day', 'week', 'month' or 'year' + * @param string|null $groupOn1 '', 'name', 'project', 'client' or 'userUid' + * @param string|null $groupOn2 same as $groupOn1 + * @param bool $admin report all users' entries + * @param int|null $start offset into the resulting rows + * @param int|null $limit maximum number of resulting rows (default 10000) + * @param \DateTimeZone|null $timeZone time zone for time groups; default: the logged-in user's + * @return ReportItem[] + */ + public function report($user, $from, $to, $filterProjectId, $filterClientId, $filterTagId, $timegroup, $groupOn1, $groupOn2, $admin, $start, $limit, ?\DateTimeZone $timeZone = null) { + $timeZone = $timeZone ?? $this->userTimeZone->get(); + $timegroup = in_array($timegroup, ['day', 'week', 'month', 'year'], true) ? $timegroup : ''; + $groupOn1 = in_array($groupOn1, self::GROUPABLE, true) ? $groupOn1 : ''; + $groupOn2 = in_array($groupOn2, self::GROUPABLE, true) ? $groupOn2 : ''; + + $rows = $this->fetchIntervals($user, $from, $to, $filterProjectId, $filterClientId, $admin); + + if ($timegroup === '' && $groupOn1 === '' && $groupOn2 === '') { + $items = array_map(fn ($row) => $this->singleItem($row), $rows); } else { - $selectFields[] = 'wi.cost as cost'; + $items = $this->aggregate($rows, $timegroup, $groupOn1, $groupOn2, $timeZone); } - $aggregation = true; - if(empty($groupOn1) && empty($groupOn2) && empty($timegroup)) { - $selectFields[] = 'min(wi.details) as "details"'; - $selectFields[] = 'min(wi.name) as "name"'; - $selectFields[] = '\'*\' as "projectId"'; - $selectFields[] = 'min(p.name) as "project"'; + $start = empty($start) ? 0 : (int)$start; + $limit = empty($limit) ? 10000 : (int)$limit; + return array_slice($items, $start, $limit); + } - $selectFields[] = '\'*\' as "clientId"'; - $selectFields[] = 'min(c.name) as "client"'; - $selectFields[] = 'min(user_uid) as "userUid"'; - $aggregation = false; - } else { - $selectFields[] = '\'*\' as "details"'; + /** + * The work intervals matching the filters, oldest first. + */ + private function fetchIntervals($user, $from, $to, $filterProjectId, $filterClientId, $admin): array { + $filters = []; + $params = []; + if (!empty($from)) { + $filters[] = '(wi.start > ?)'; + $params[] = (int)$from; } - - if (!$aggregation) { - $selectFields[]= "start as time"; - } else { - $selectFields[]= "min(start) as time"; + if (!empty($to)) { + $filters[] = '(wi.start < ?)'; + $params[] = (int)$to; } - - if ($this->dbengine == 'POSTGRES') { - if ($timegroup == 'week') { - $selectFields[]= "concat(date_part('year', to_timestamp(start)), 'W', to_char(to_timestamp(start), 'IW')) as ftime"; - }elseif ($timegroup == 'year') { - $selectFields[]= "date_part('year', to_timestamp(start)) as ftime"; - }elseif ($timegroup == 'month') { - $selectFields[]= "to_char(to_timestamp(start), 'YYYY-MM') as ftime"; - }elseif ($timegroup == 'day') { - $selectFields[]= "to_char(to_timestamp(start), 'YYYY-MM-DD') as ftime"; + foreach ([[$filterProjectId, 'wi.project_id'], [$filterClientId, 'c.id']] as [$ids, $column]) { + if (empty($ids) || !is_array($ids)) { + continue; } - } else if ($this->dbengine == 'SQLITE') { - if ($timegroup == 'week') { - $selectFields[]= "strftime('%YW%W', datetime(start, 'unixepoch')) as ftime"; - }elseif ($timegroup == 'year') { - $selectFields[]= "strftime('%Y', datetime(start, 'unixepoch')) as ftime"; - }elseif ($timegroup == 'month') { - $selectFields[]= "strftime('%Y-%m', datetime(start, 'unixepoch')) as ftime"; - }elseif ($timegroup == 'day') { - $selectFields[]= "strftime('%Y-%m-%d', datetime(start, 'unixepoch')) as ftime"; - } - } else { - if ($timegroup == 'week') { - $selectFields[]= "CONCAT(YEAR(FROM_UNIXTIME(start)), 'W', WEEK(FROM_UNIXTIME(start))) as ftime"; - }elseif ($timegroup == 'year') { - $selectFields[]= "YEAR(FROM_UNIXTIME(start)) as ftime"; - }elseif ($timegroup == 'month') { - $selectFields[]= "DATE_FORMAT(FROM_UNIXTIME(start),'%Y-%m') as ftime"; - }elseif ($timegroup == 'day') { - $selectFields[]= "DATE(FROM_UNIXTIME(start)) as ftime"; + $values = array_values(array_filter($ids, fn ($id) => $id !== null && $id !== '')); + $orNull = count($values) !== count($ids) ? " or $column is null" : ''; + $in = $values ? "$column in (" . implode(',', array_fill(0, count($values), '?')) . ')' : '1 = 0'; + $filters[] = "($in$orNull)"; + foreach ($values as $v) { + $params[] = (int)$v; } } - - if ($aggregation){ - if($groupOn1 != 'name'){ - if ($this->dbengine != 'MYSQL') { - $selectFields[] = '\'*\' as name'; - } else { - $selectFields[] = 'CASE WHEN CHAR_LENGTH(group_concat(distinct wi.name)) > 40 THEN CONCAT(SUBSTRING(group_concat(distinct wi.name), 1, 40), "...") ELSE group_concat(distinct wi.name) END as name'; - } - //$selectFields[] = 'group_concat(distinct wi.name) as name'; - } else { - $selectFields[] = 'wi.name as name'; - } + if (!empty($user) && !$admin) { + $filters[] = '(wi.user_uid = ?)'; + $params[] = $user; } + $sql = 'SELECT wi.id, wi.name, wi.details, wi.start, wi.duration, wi.cost, wi.user_uid,' + . ' p.name AS project_name, c.name AS client_name' + . ' FROM *PREFIX*timetracker_work_interval wi' + . ' LEFT JOIN *PREFIX*timetracker_project p ON wi.project_id = p.id' + . ' LEFT JOIN *PREFIX*timetracker_client c ON p.client_id = c.id' + . ($filters ? ' WHERE ' . implode(' AND ', $filters) : '') + . ' ORDER BY wi.start ASC, wi.id ASC'; - if ($aggregation){ - if(($groupOn1 != 'project') && ($groupOn2 != 'project')){ - $selectFields[] = '\'*\' as "projectId"'; - if ($this->dbengine == 'POSTGRES') { - $selectFields[] = 'string_agg(distinct p.name, \',\') as project'; - } else { - $selectFields[] = 'group_concat(distinct p.name) as project'; - } - } else { - - $selectFields[] = '\'*\' as "projectId"'; - $selectFields[] = 'p.name as project'; - } - + $result = $this->db->executeQuery($sql, $params); + $rows = $result->fetchAll(); + $result->closeCursor(); + return $rows; + } - if(($groupOn1 != 'client') && ($groupOn2 != 'client')){ - $selectFields[] = '\'*\' as "clientId"'; - if ($this->dbengine == 'POSTGRES') { - $selectFields[] = 'string_agg(distinct c.name, \',\') as client'; - } else { - $selectFields[] = 'group_concat(distinct c.name) as client'; - } + /** + * One report row per work interval (no grouping). + */ + private function singleItem(array $row): ReportItem { + return ReportItem::fromRow([ + 'id' => (int)$row['id'], + 'name' => $row['name'], + 'details' => $row['details'], + 'projectId' => '*', + 'clientId' => '*', + 'project' => $row['project_name'], + 'client' => $row['client_name'], + 'userUid' => $row['user_uid'], + 'time' => (string)$row['start'], + 'totalDuration' => (int)$row['duration'], + 'cost' => $row['cost'] === null ? null : (int)$row['cost'], + ]); + } - } else { - $selectFields[] = '\'*\' as "clientId"'; - $selectFields[] = 'c.name as client'; + /** + * Group the rows by time group and up to two columns, in order of each + * group's first entry. + * + * @return ReportItem[] + */ + private function aggregate(array $rows, string $timegroup, string $groupOn1, string $groupOn2, \DateTimeZone $timeZone): array { + $groupedBy = array_values(array_filter([$groupOn1, $groupOn2])); + $groups = []; + foreach ($rows as $row) { + $values = [ + 'name' => $row['name'], + 'project' => $row['project_name'], + 'client' => $row['client_name'], + 'userUid' => $row['user_uid'], + ]; + $ftime = $timegroup !== '' ? self::timeGroupLabel((int)$row['start'], $timegroup, $timeZone) : null; + $key = json_encode([$ftime, ...array_map(fn ($g) => $values[$g], $groupedBy)]); + + if (!isset($groups[$key])) { + $groups[$key] = [ + 'id' => (int)$row['id'], + 'time' => (int)$row['start'], + 'ftime' => $ftime, + 'totalDuration' => 0, + 'cost' => 0, + 'name' => [], 'project' => [], 'client' => [], 'userUid' => [], + ]; } - - if(($groupOn1 != 'userUid') && ($groupOn2 != 'userUid') && $aggregation){ - if ($this->dbengine == 'POSTGRES') { - $selectFields[] = 'string_agg(distinct user_uid, \',\') as "userUid"'; - } else { - $selectFields[] = 'group_concat(distinct user_uid) as "userUid"'; + $group = &$groups[$key]; + $group['id'] = min($group['id'], (int)$row['id']); + $group['time'] = min($group['time'], (int)$row['start']); + $group['totalDuration'] += (int)$row['duration']; + $group['cost'] += (int)$row['cost']; + foreach (self::GROUPABLE as $column) { + // distinct values in order of appearance, like a database's group_concat(distinct ...) + if ($values[$column] !== null && !in_array($values[$column], $group[$column], true)) { + $group[$column][] = $values[$column]; } - - } else { - $selectFields[] = 'user_uid as "userUid"'; } - - } - - $selectItems = implode(", ",$selectFields). - ' FROM *PREFIX*timetracker_work_interval wi - left join *PREFIX*timetracker_project p on wi.project_id = p.id - left join *PREFIX*timetracker_client c on p.client_id = c.id'; - $filters = []; - $params = []; - if (!empty($from)){ - $filters[] = "(start > ?)"; - $params[] = $from; - + unset($group); } - if (!empty($to)){ - $filters[] = "(start < ?)"; - $params[] = $to; - } - if (!empty($filterProjectId)){ - $qm = []; - $append = ''; - foreach($filterProjectId as $f){ - $qm[] = '?'; - $params[] = $f; - - if($f == null) { - $append = ' or wi.project_id is null '; - } + $items = []; + foreach ($groups as $group) { + $list = fn ($column) => $group[$column] ? implode(',', $group[$column]) : null; + $name = $list('name'); + if (!in_array('name', $groupedBy, true) && $name !== null && mb_strlen($name) > self::MAX_NAME_LIST_LENGTH) { + $name = mb_substr($name, 0, self::MAX_NAME_LIST_LENGTH) . '...'; } - $filters[] = '(wi.project_id in ('.implode(",",$qm).')'.$append.')'; - } - if (!empty($filterClientId)){ - $qm = []; - $append = ''; - foreach($filterClientId as $f){ - $qm[] = '?'; - $params[] = $f; - if ($f == null) { - $append = ' or c.id is null '; - } - } - $filters[] = '(c.id in ('.implode(",",$qm).')'.$append.')'; - } - if ( (!empty($user)) && (!$admin) ){ - $filters[] = "(user_uid = ?)"; - $params[] = $user; - - } - $group = ''; - $groups = []; - if (!empty($timegroup)){ - // if($timegroup == 'week'){ - // $groups[] = "YEARWEEK(start)"; - // } elseif ($timegroup == 'day') { - // $groups[] = "DATE(start)"; - // } elseif ($timegroup == 'month') { - // $groups[] = "EXTRACT(YEAR_MONTH FROM start)"; - // } elseif ($timegroup == 'year') { - // $groups[] = "YEAR(start)"; - // } - $groups[] = 'ftime'; - } - - if (!empty($groupOn1)){ - if ($groupOn1 == "project" || $groupOn1 == "client" || $groupOn1 == "name" || $groupOn1 == "userUid") - // $groups[] = $groupOn1; - if ($groupOn1 == 'name'){ - $groups[] = 'wi.name'; - } else { - if ($this->dbengine == 'POSTGRES') { // postgres needs quotes on names - $groups[] = '"'.$groupOn1.'"'; - } else { - $groups[] = $groupOn1; - } - } - } - if (!empty($groupOn2)){ - if ($groupOn2 == "project" || $groupOn2 == "client" || $groupOn2 == "name" || $groupOn2 == "userUid"){ - if ($groupOn2 == 'name'){ - $groups[] = 'wi.name'; - } else { - if ($this->dbengine == 'POSTGRES') { // postgres needs quotes on names - $groups[] = '"'.$groupOn2.'"'; - } else { - $groups[] = $groupOn2; - } - } + $item = [ + 'id' => $group['id'], + 'name' => $name, + 'details' => '*', + 'projectId' => '*', + 'clientId' => '*', + 'project' => $list('project'), + 'client' => $list('client'), + 'userUid' => $list('userUid'), + 'time' => (string)$group['time'], + 'totalDuration' => $group['totalDuration'], + 'cost' => $group['cost'], + ]; + if ($group['ftime'] !== null) { + $item['ftime'] = $group['ftime']; } + $items[] = ReportItem::fromRow($item); } + return $items; + } - if (!empty($groups)){ - $group = "group by ".implode(",",$groups); - } else { - $group = "group by wi.id"; - } - if (empty($start)){ - $start = 0; - } - if (empty($limit)){ - $limit = 10000; + /** + * The label of the time group a moment falls into, in the given time zone: + * day "2025-01-31", week "2025-W05" (ISO 8601), month "2025-01", year "2025". + */ + public static function timeGroupLabel(int $timestamp, string $timegroup, \DateTimeZone $timeZone): string { + $date = (new \DateTimeImmutable('@' . $timestamp))->setTimezone($timeZone); + switch ($timegroup) { + case 'week': + return $date->format('o-\WW'); + case 'month': + return $date->format('Y-m'); + case 'year': + return $date->format('Y'); + default: + return $date->format('Y-m-d'); } - $sql = 'SELECT '.$selectItems.' where '.implode(" and ",$filters).' '.$group; - //var_dump($sql); - // var_dump($params); - return $this->findEntities($sql, $params, $limit, $start); } - - - } diff --git a/lib/Service/UserTimeZone.php b/lib/Service/UserTimeZone.php new file mode 100644 index 0000000..5828ecf --- /dev/null +++ b/lib/Service/UserTimeZone.php @@ -0,0 +1,48 @@ +config = $config; + $this->userSession = $userSession; + } + + /** + * @param string|null $userId default: the logged-in user + */ + public function get(?string $userId = null): \DateTimeZone { + if ($userId === null) { + $user = $this->userSession->getUser(); + $userId = $user === null ? null : $user->getUID(); + } + $name = $userId === null ? '' : $this->config->getUserValue($userId, 'core', 'timezone', ''); + foreach ([$name, $this->config->getSystemValueString('default_timezone', 'UTC')] as $candidate) { + if ($candidate === '') { + continue; + } + try { + return new \DateTimeZone($candidate); + } catch (\Exception $e) { + // not a valid time zone name, try the next one + } + } + return new \DateTimeZone('UTC'); + } +} diff --git a/tests/Integration/AppTest.php b/tests/Integration/AppTest.php index 708bafa..59f12cd 100644 --- a/tests/Integration/AppTest.php +++ b/tests/Integration/AppTest.php @@ -22,7 +22,7 @@ public function setUp(): void { } public function testAppInstalled(): void { - $appManager = $this->container->query('OCP\App\IAppManager'); + $appManager = $this->container->get(\OCP\App\IAppManager::class); $this->assertTrue($appManager->isInstalled('timetracker')); } diff --git a/tests/Integration/Db/ReportItemMapperTest.php b/tests/Integration/Db/ReportItemMapperTest.php new file mode 100644 index 0000000..c8215c2 --- /dev/null +++ b/tests/Integration/Db/ReportItemMapperTest.php @@ -0,0 +1,346 @@ +db = \OCP\Server::get(IDBConnection::class); + $this->mapper = \OCP\Server::get(ReportItemMapper::class); + $this->prefix = 'tt-it-' . bin2hex(random_bytes(4)) . '-'; + } + + protected function tearDown(): void { + $qb = $this->db->getQueryBuilder(); + $qb->delete('timetracker_work_interval') + ->where($qb->expr()->like('user_uid', $qb->createNamedParameter($this->prefix . '%'))) + ->executeStatement(); + if ($this->projectIds) { + $qb = $this->db->getQueryBuilder(); + $qb->delete('timetracker_project') + ->where($qb->expr()->in('id', $qb->createNamedParameter($this->projectIds, IQueryBuilder::PARAM_INT_ARRAY))) + ->executeStatement(); + } + if ($this->clientIds) { + $qb = $this->db->getQueryBuilder(); + $qb->delete('timetracker_client') + ->where($qb->expr()->in('id', $qb->createNamedParameter($this->clientIds, IQueryBuilder::PARAM_INT_ARRAY))) + ->executeStatement(); + } + parent::tearDown(); + } + + // --- time groups --- + + public function testDayGroupsFollowTheUserTimeZone(): void { + $user = $this->user('alice'); + // 23:30 and 00:30 in Berlin (UTC+1 in March before the switch) + $this->entry($user, 'Late', '2026-03-10 22:30', 1800); + $this->entry($user, 'Early', '2026-03-10 23:30', 1800); + + $this->assertSame( + [['2026-03-10', 1800], ['2026-03-11', 1800]], + $this->pick($this->report($user, 'day', '', '', self::BERLIN), 'ftime', 'totalDuration') + ); + $this->assertSame( + [['2026-03-10', 3600]], + $this->pick($this->report($user, 'day', '', '', 'UTC'), 'ftime', 'totalDuration') + ); + } + + public function testDayGroupsAcrossTheSwitchToSummerTime(): void { + $user = $this->user('alice'); + // Berlin switches to UTC+2 on 29 March 2026: 23:30 on the 29th, then 00:30 on the 30th + $this->entry($user, 'Before midnight', '2026-03-29 21:30', 600); + $this->entry($user, 'After midnight', '2026-03-29 22:30', 600); + + $this->assertSame( + ['2026-03-29', '2026-03-30'], + array_column($this->pick($this->report($user, 'day', '', '', self::BERLIN), 'ftime'), 0) + ); + } + + public function testIsoWeeksAcrossTheYearBoundary(): void { + $user = $this->user('alice'); + $this->entry($user, 'Sunday', '2024-12-29 12:00', 100); // last day of 2024-W52 + $this->entry($user, 'Monday', '2024-12-30 12:00', 200); // first day of 2025-W01 + $this->entry($user, 'Sunday', '2025-01-05 12:00', 300); // last day of 2025-W01 + $this->entry($user, 'Monday', '2025-01-06 12:00', 400); // first day of 2025-W02 + + $this->assertSame( + [['2024-W52', 100], ['2025-W01', 500], ['2025-W02', 400]], + $this->pick($this->report($user, 'week', '', '', self::BERLIN), 'ftime', 'totalDuration') + ); + } + + public function testMonthsAndYearsAtNewYearInTheUserTimeZone(): void { + $user = $this->user('alice'); + $this->entry($user, 'Old year', '2025-12-31 12:00', 100); + $this->entry($user, 'New year', '2025-12-31 23:30', 200); // 00:30 on 1 January in Berlin + + $this->assertSame( + [['2025-12', 100], ['2026-01', 200]], + $this->pick($this->report($user, 'month', '', '', self::BERLIN), 'ftime', 'totalDuration') + ); + $this->assertSame( + [['2025', 100], ['2026', 200]], + $this->pick($this->report($user, 'year', '', '', self::BERLIN), 'ftime', 'totalDuration') + ); + $this->assertSame( + [['2025', 300]], + $this->pick($this->report($user, 'year', '', '', 'UTC'), 'ftime', 'totalDuration') + ); + } + + public function testTimeGroupLabels(): void { + $berlin = new \DateTimeZone(self::BERLIN); + $at = fn (string $utc) => (new \DateTimeImmutable($utc, new \DateTimeZone('UTC')))->getTimestamp(); + $this->assertSame('2025-W01', ReportItemMapper::timeGroupLabel($at('2024-12-30 00:00'), 'week', $berlin)); + $this->assertSame('2026-W53', ReportItemMapper::timeGroupLabel($at('2026-12-31 12:00'), 'week', $berlin)); + $this->assertSame('2020-W53', ReportItemMapper::timeGroupLabel($at('2021-01-03 12:00'), 'week', $berlin)); + $this->assertSame('2026-01-01', ReportItemMapper::timeGroupLabel($at('2025-12-31 23:00'), 'day', $berlin)); + $this->assertSame('2025-12-31', ReportItemMapper::timeGroupLabel($at('2025-12-31 23:00'), 'day', new \DateTimeZone('UTC'))); + } + + // --- grouping by columns --- + + public function testGroupByProjectSumsDurationAndCost(): void { + $user = $this->user('alice'); + $x = $this->client('Client X'); + $y = $this->client('Client Y'); + $a = $this->project('Project A', $x); + $b = $this->project('Project B', $y); + $this->entry($user, 'Coding', '2026-02-02 08:00', 600, $a, 100); + $this->entry($user, 'Review', '2026-02-02 09:00', 300, $b); + $this->entry($user, 'Coding', '2026-02-02 10:00', 1200, $a, 250); + $this->entry($user, 'Lunch', '2026-02-02 11:00', 60); + + $this->assertSame( + [ + ['Project A', 'Client X', 1800, 350, 'Coding'], + ['Project B', 'Client Y', 300, 0, 'Review'], + [null, null, 60, 0, 'Lunch'], + ], + $this->pick($this->report($user, '', 'project', '', self::BERLIN), 'project', 'client', 'totalDuration', 'cost', 'name') + ); + } + + public function testGroupByClientListsItsProjects(): void { + $user = $this->user('alice'); + $x = $this->client('Client X'); + $a = $this->project('Project A', $x); + $b = $this->project('Project B', $x); + $this->entry($user, 'One', '2026-02-02 08:00', 100, $a); + $this->entry($user, 'Two', '2026-02-02 09:00', 200, $b); + $this->entry($user, 'Three', '2026-02-02 10:00', 300, $a); + + $this->assertSame( + [['Client X', 'Project A,Project B', 600, 'One,Two,Three']], + $this->pick($this->report($user, '', 'client', '', self::BERLIN), 'client', 'project', 'totalDuration', 'name') + ); + } + + public function testGroupByUserReportsAllUsersOnlyForAdmins(): void { + $alice = $this->user('alice'); + $bob = $this->user('bob'); + $a = $this->project('Project A'); + $this->entry($alice, 'Work', '2026-02-02 08:00', 100, $a); + $this->entry($bob, 'Work', '2026-02-02 09:00', 200, $a); + $this->entry($alice, 'Work', '2026-02-02 10:00', 300, $a); + + $this->assertSame( + [[$alice, 400], [$bob, 200]], + $this->pick($this->mapper->report($alice, null, null, [$a], [], [], '', 'userUid', '', true, 0, 0, new \DateTimeZone('UTC')), 'userUid', 'totalDuration') + ); + $this->assertSame( + [[$alice, 400]], + $this->pick($this->mapper->report($alice, null, null, [$a], [], [], '', 'userUid', '', false, 0, 0, new \DateTimeZone('UTC')), 'userUid', 'totalDuration') + ); + } + + public function testTwoLevelGroupingWithTimeGroup(): void { + $user = $this->user('alice'); + $a = $this->project('Project A'); + $b = $this->project('Project B'); + $this->entry($user, 'Coding', '2026-02-02 08:00', 100, $a); + $this->entry($user, 'Review', '2026-02-02 09:00', 200, $a); + $this->entry($user, 'Coding', '2026-02-02 10:00', 300, $b); + $this->entry($user, 'Coding', '2026-02-03 08:00', 400, $a); + + $this->assertSame( + [ + ['2026-02-02', 'Project A', 'Coding', 100], + ['2026-02-02', 'Project A', 'Review', 200], + ['2026-02-02', 'Project B', 'Coding', 300], + ['2026-02-03', 'Project A', 'Coding', 400], + ], + $this->pick($this->report($user, 'day', 'project', 'name', self::BERLIN), 'ftime', 'project', 'name', 'totalDuration') + ); + } + + public function testAggregatedNamesAreDistinctAndShortened(): void { + $user = $this->user('alice'); + $a = $this->project('Project A'); + foreach (['Alpha task', 'Beta task', 'Alpha task', 'Gamma task', 'Delta task', 'Epsilon'] as $i => $name) { + $this->entry($user, $name, '2026-02-02 0' . $i . ':00', 60, $a); + } + + $items = $this->report($user, '', 'project', '', self::BERLIN); + $this->assertSame('Alpha task,Beta task,Gamma task,Delta ta...', $items[0]->name); + $this->assertSame(360, $items[0]->totalDuration); + + // grouped by name, names are never shortened or combined + $this->assertSame( + ['Alpha task', 'Beta task', 'Gamma task', 'Delta task', 'Epsilon'], + array_column($this->pick($this->report($user, '', 'name', '', self::BERLIN), 'name'), 0) + ); + } + + // --- without grouping, filters --- + + public function testWithoutGroupingEveryEntryIsOneRow(): void { + $user = $this->user('alice'); + $a = $this->project('Project A', $this->client('Client X')); + $this->entry($user, 'Second', '2026-02-02 10:00', 200, $a, 50, 'Some details'); + $this->entry($user, 'First', '2026-02-02 08:00', 100); + + $start = (new \DateTimeImmutable('2026-02-02 08:00', new \DateTimeZone('UTC')))->getTimestamp(); + $this->assertSame( + [ + ['First', '', null, null, (string)$start, 100, null], + ['Second', 'Some details', 'Project A', 'Client X', (string)($start + 7200), 200, 50], + ], + $this->pick($this->report($user, '', '', '', self::BERLIN), 'name', 'details', 'project', 'client', 'time', 'totalDuration', 'cost') + ); + } + + public function testProjectFilterCanIncludeEntriesWithoutProject(): void { + $user = $this->user('alice'); + $a = $this->project('Project A'); + $b = $this->project('Project B'); + $this->entry($user, 'On A', '2026-02-02 08:00', 100, $a); + $this->entry($user, 'On B', '2026-02-02 09:00', 200, $b); + $this->entry($user, 'No project', '2026-02-02 10:00', 300); + + $names = fn (array $filter) => array_column($this->pick( + $this->mapper->report($user, null, null, $filter, [], [], '', '', '', false, 0, 0, new \DateTimeZone('UTC')), + 'name' + ), 0); + $this->assertSame(['On A'], $names([$a])); + $this->assertSame(['On A', 'No project'], $names([$a, null])); + $this->assertSame(['On A', 'On B', 'No project'], $names([])); + } + + public function testDateRangeExcludesItsBounds(): void { + $user = $this->user('alice'); + $at = fn (string $utc) => (new \DateTimeImmutable($utc, new \DateTimeZone('UTC')))->getTimestamp(); + $this->entry($user, 'Before', '2026-02-01 12:00', 100); + $this->entry($user, 'Inside', '2026-02-02 12:00', 200); + $this->entry($user, 'At end', '2026-02-03 12:00', 300); + + $items = $this->mapper->report($user, $at('2026-02-01 12:00'), $at('2026-02-03 12:00'), [], [], [], '', '', '', false, 0, 0, new \DateTimeZone('UTC')); + $this->assertSame(['Inside'], array_column($this->pick($items, 'name'), 0)); + } + + public function testOffsetAndLimitApplyToTheResultingRows(): void { + $user = $this->user('alice'); + foreach (['2026-02-02', '2026-02-03', '2026-02-04', '2026-02-05'] as $day) { + $this->entry($user, 'Work', $day . ' 08:00', 100); + $this->entry($user, 'Work', $day . ' 09:00', 100); + } + $items = $this->mapper->report($user, null, null, [], [], [], 'day', '', '', false, 1, 2, new \DateTimeZone('UTC')); + $this->assertSame([['2026-02-03', 200], ['2026-02-04', 200]], $this->pick($items, 'ftime', 'totalDuration')); + } + + // --- helpers --- + + /** + * @return ReportItem[] + */ + private function report(string $user, string $timegroup, string $group1, string $group2, string $timeZone): array { + return $this->mapper->report($user, null, null, [], [], [], $timegroup, $group1, $group2, false, 0, 0, new \DateTimeZone($timeZone)); + } + + /** + * The given fields of each item, as lists. + * + * @param ReportItem[] $items + */ + private function pick(array $items, string ...$fields): array { + return array_map(fn (ReportItem $item) => array_map(fn ($f) => $item->$f, $fields), $items); + } + + private function user(string $name): string { + return $this->prefix . $name; + } + + private function client(string $name): int { + $qb = $this->db->getQueryBuilder(); + $qb->insert('timetracker_client')->values([ + 'name' => $qb->createNamedParameter($name), + 'created_at' => $qb->createNamedParameter(time(), IQueryBuilder::PARAM_INT), + ])->executeStatement(); + return $this->clientIds[] = $qb->getLastInsertId(); + } + + private function project(string $name, ?int $clientId = null): int { + $qb = $this->db->getQueryBuilder(); + $qb->insert('timetracker_project')->values([ + 'name' => $qb->createNamedParameter($name), + 'client_id' => $qb->createNamedParameter($clientId, $clientId === null ? IQueryBuilder::PARAM_NULL : IQueryBuilder::PARAM_INT), + 'created_by_user_uid' => $qb->createNamedParameter($this->prefix . 'admin'), + 'created_at' => $qb->createNamedParameter(time(), IQueryBuilder::PARAM_INT), + ])->executeStatement(); + return $this->projectIds[] = $qb->getLastInsertId(); + } + + /** + * @param string $startUtc e.g. "2026-03-10 22:30", in UTC + */ + private function entry(string $user, string $name, string $startUtc, int $duration, ?int $projectId = null, ?int $cost = null, string $details = ''): void { + $start = (new \DateTimeImmutable($startUtc, new \DateTimeZone('UTC')))->getTimestamp(); + $qb = $this->db->getQueryBuilder(); + $qb->insert('timetracker_work_interval')->values([ + 'name' => $qb->createNamedParameter($name), + 'details' => $qb->createNamedParameter($details), + 'project_id' => $qb->createNamedParameter($projectId, $projectId === null ? IQueryBuilder::PARAM_NULL : IQueryBuilder::PARAM_INT), + 'user_uid' => $qb->createNamedParameter($user), + 'start' => $qb->createNamedParameter($start, IQueryBuilder::PARAM_INT), + 'duration' => $qb->createNamedParameter($duration, IQueryBuilder::PARAM_INT), + 'running' => $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT), + 'cost' => $qb->createNamedParameter($cost, $cost === null ? IQueryBuilder::PARAM_NULL : IQueryBuilder::PARAM_INT), + ])->executeStatement(); + } +} diff --git a/tests/Integration/Service/UserTimeZoneTest.php b/tests/Integration/Service/UserTimeZoneTest.php new file mode 100644 index 0000000..db15ca8 --- /dev/null +++ b/tests/Integration/Service/UserTimeZoneTest.php @@ -0,0 +1,64 @@ +config = \OCP\Server::get(IConfig::class); + $this->user = 'tt-it-' . bin2hex(random_bytes(4)); + $this->defaultTimeZone = $this->config->getSystemValue('default_timezone', null); + } + + protected function tearDown(): void { + $this->config->deleteUserValue($this->user, 'core', 'timezone'); + if ($this->defaultTimeZone === null) { + $this->config->deleteSystemValue('default_timezone'); + } else { + $this->config->setSystemValue('default_timezone', $this->defaultTimeZone); + } + parent::tearDown(); + } + + private function timeZone(): string { + return \OCP\Server::get(UserTimeZone::class)->get($this->user)->getName(); + } + + public function testUsesTheUserSetting(): void { + $this->config->setSystemValue('default_timezone', 'America/New_York'); + $this->config->setUserValue($this->user, 'core', 'timezone', 'Europe/Berlin'); + $this->assertSame('Europe/Berlin', $this->timeZone()); + } + + public function testFallsBackToTheServerDefault(): void { + $this->config->setSystemValue('default_timezone', 'America/New_York'); + $this->assertSame('America/New_York', $this->timeZone()); + + $this->config->setUserValue($this->user, 'core', 'timezone', 'Not/AZone'); + $this->assertSame('America/New_York', $this->timeZone()); + } + + public function testFallsBackToUtc(): void { + $this->config->deleteSystemValue('default_timezone'); + $this->assertSame('UTC', $this->timeZone()); + + $this->config->setSystemValue('default_timezone', 'Not/AZone'); + $this->assertSame('UTC', $this->timeZone()); + } +} diff --git a/tests/bootstrap.integration.php b/tests/bootstrap.integration.php index d09c421..dea9837 100644 --- a/tests/bootstrap.integration.php +++ b/tests/bootstrap.integration.php @@ -1,15 +1,17 @@ /apps/timetracker, installed and enabled. + if (!defined('PHPUNIT_RUN')) { define('PHPUNIT_RUN', 1); } require_once __DIR__ . '/../../../lib/base.php'; -// Fix for "Autoload path not allowed: .../tests/lib/testcase.php" -\OC::$loader->addValidRoot(OC::$SERVERROOT . '/tests'); +// Makes Test\TestCase from the server's tests available +require_once __DIR__ . '/../../../tests/autoload.php'; -// Fix for "Autoload path not allowed: .../timetracker/tests/testcase.php" -\OC_App::loadApp('timetracker'); +\OCP\Server::get(\OCP\App\IAppManager::class)->loadApp('timetracker'); OC_Hook::clear();