Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   1  <?php
   2  // This file is part of Moodle - http://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 <http://www.gnu.org/licenses/>.
  16  
  17  namespace core_user;
  18  
  19  use stdClass;
  20  
  21  /**
  22   * Tests for the devicekey class.
  23   *
  24   * @package core_user
  25   * @covers \core_user\devicekey
  26   * @requires extension sodium
  27   */
  28  class devicekey_test extends \advanced_testcase {
  29  
  30      /**
  31       * Helper to create a device record.
  32       *
  33       * @return stdClass
  34       */
  35      protected function create_device_record(): stdClass {
  36          global $USER, $DB;
  37  
  38          $device = (object) [
  39              'appid' => 'com.moodle.moodlemobile',
  40              'name' => 'occam',
  41              'model' => 'Nexus 4',
  42              'platform' => 'Android',
  43              'version' => '4.2.2',
  44              'pushid' => 'apushdkasdfj4835',
  45              'uuid' => 'ABCDE3723ksdfhasfaasef859',
  46              'userid' => $USER->id,
  47              'timecreated' => time(),
  48              'timemodified' => time(),
  49          ];
  50          $device->id = $DB->insert_record('user_devices', $device);
  51  
  52          return $device;
  53      }
  54  
  55      public function test_update_device_public_key_no_device(): void {
  56          global $DB;
  57  
  58          $this->resetAfterTest();
  59          $this->setAdminUser();
  60  
  61          $device = $this->create_device_record();
  62  
  63          $devicekeypair = sodium_crypto_box_keypair();
  64          $publickey = sodium_bin2base64(
  65              sodium_crypto_box_publickey($devicekeypair),
  66              SODIUM_BASE64_VARIANT_ORIGINAL
  67          );
  68  
  69          $this->assertTrue(devicekey::update_device_public_key($device->uuid, $device->appid, $publickey));
  70          $this->assertEquals($publickey, $DB->get_field('user_devices', 'publickey', ['id' => $device->id]));
  71      }
  72  }