Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [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   */
  27  class devicekey_test extends \advanced_testcase {
  28      /**
  29       * Helper to create a device record.
  30       *
  31       * @return stdClass
  32       */
  33      protected function create_device_record(): stdClass {
  34          global $USER, $DB;
  35  
  36          $device = (object) [
  37              'appid' => 'com.moodle.moodlemobile',
  38              'name' => 'occam',
  39              'model' => 'Nexus 4',
  40              'platform' => 'Android',
  41              'version' => '4.2.2',
  42              'pushid' => 'apushdkasdfj4835',
  43              'uuid' => 'ABCDE3723ksdfhasfaasef859',
  44              'userid' => $USER->id,
  45              'timecreated' => time(),
  46              'timemodified' => time(),
  47          ];
  48          $device->id = $DB->insert_record('user_devices', $device);
  49  
  50          return $device;
  51      }
  52  
  53      public function test_update_device_public_key_no_device(): void {
  54          global $DB;
  55  
  56          $this->resetAfterTest();
  57          $this->setAdminUser();
  58  
  59          $device = $this->create_device_record();
  60  
  61          $devicekeypair = sodium_crypto_box_keypair();
  62          $publickey = sodium_bin2base64(
  63              sodium_crypto_box_publickey($devicekeypair),
  64              SODIUM_BASE64_VARIANT_ORIGINAL
  65          );
  66  
  67          $this->assertTrue(devicekey::update_device_public_key($device->uuid, $device->appid, $publickey));
  68          $this->assertEquals($publickey, $DB->get_field('user_devices', 'publickey', ['id' => $device->id]));
  69      }
  70  }