Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
message_webpush_generator
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 create_subscription
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2// This file is part of Moodle - https://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <https://www.gnu.org/licenses/>.
16
17/**
18 * Test data generator.
19 *
20 * @package    message_webpush
21 * @copyright  2026 DevLMS
22 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24class message_webpush_generator extends component_generator_base {
25    /** @var int Counter for distinct endpoints. */
26    private int $count = 0;
27
28    /**
29     * A browser with notifications on, with real keys and a push service address.
30     *
31     * @param array $record userid required; label, timeacked, pendingsince optional
32     * @return stdClass
33     */
34    public function create_subscription(array $record): stdClass {
35        global $DB;
36        $this->count++;
37        $sub = \message_webpush\local\subscriptions::add(
38            (int)$record['userid'],
39            $record['endpoint'] ?? 'https://fcm.googleapis.com/fcm/send/generated-' . $this->count . '-' . random_string(8),
40            \message_webpush\local\crypto::b64u_encode(\message_webpush\local\crypto::generate_keys()['public']),
41            \message_webpush\local\crypto::b64u_encode(random_bytes(16)),
42            $record['label'] ?? 'Chrome / Windows'
43        );
44        foreach (['timeacked', 'pendingsince'] as $field) {
45            if (isset($record[$field])) {
46                $DB->set_field('message_webpush_subs', $field, (int)$record[$field], ['id' => $sub->id]);
47                $sub->$field = (int)$record[$field];
48            }
49        }
50        return $sub;
51    }
52}