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\external;
  18  
  19  global $CFG;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  require_once("{$CFG->libdir}/externallib.php");
  23  
  24  use external_api;
  25  use stdClass;
  26  
  27  /**
  28   * Tests for the devicekey class.
  29   *
  30   * @package core_user
  31   * @covers \core_user\external\update_user_device_public_key
  32   * @requires extension sodium
  33   */
  34  class update_user_device_public_key_test extends \advanced_testcase {
  35      /**
  36       * Helper to create a device record.
  37       *
  38       * @return stdClass
  39       */
  40      protected function create_device_record(): stdClass {
  41          global $USER, $DB;
  42  
  43          $device = (object) [
  44              'appid' => 'com.moodle.moodlemobile',
  45              'name' => 'occam',
  46              'model' => 'Nexus 4',
  47              'platform' => 'Android',
  48              'version' => '4.2.2',
  49              'pushid' => 'apushdkasdfj4835',
  50              'uuid' => 'ABCDE3723ksdfhasfaasef859',
  51              'userid' => $USER->id,
  52              'timecreated' => time(),
  53              'timemodified' => time(),
  54          ];
  55          $device->id = $DB->insert_record('user_devices', $device);
  56  
  57          return $device;
  58      }
  59  
  60      public function test_execute(): void {
  61          $this->resetAfterTest();
  62          $this->setAdminUser();
  63  
  64          $device = $this->create_device_record();
  65  
  66          $devicekeypair = sodium_crypto_box_keypair();
  67          $publickey = sodium_bin2base64(
  68              sodium_crypto_box_publickey($devicekeypair),
  69              SODIUM_BASE64_VARIANT_ORIGINAL
  70          );
  71  
  72          // Test sending a key to a valid device.
  73          $result = update_user_device_public_key::execute(
  74              $device->uuid,
  75              $device->appid,
  76              $publickey,
  77          );
  78  
  79          $result = external_api::clean_returnvalue(update_user_device_public_key::execute_returns(), $result);
  80          $this->assertTrue($result['status']);
  81          $this->assertEmpty($result['warnings']);
  82      }
  83  
  84      public function test_execute_with_invalid_device_appid(): void {
  85          $this->resetAfterTest();
  86          $this->setAdminUser();
  87  
  88          $device = $this->create_device_record();
  89  
  90          $devicekeypair = sodium_crypto_box_keypair();
  91          $publickey = sodium_bin2base64(
  92              sodium_crypto_box_publickey($devicekeypair),
  93              SODIUM_BASE64_VARIANT_ORIGINAL
  94          );
  95  
  96          // Invalid appid.
  97          $result = update_user_device_public_key::execute(
  98              $device->uuid,
  99              'invalidappid',
 100              $publickey,
 101          );
 102  
 103          $result = external_api::clean_returnvalue(update_user_device_public_key::execute_returns(), $result);
 104          $this->assertFalse($result['status']);
 105          $this->assertNotEmpty($result['warnings']);
 106      }
 107  
 108      public function test_execute_with_invalid_device_uuid(): void {
 109          $this->resetAfterTest();
 110          $this->setAdminUser();
 111  
 112          $device = $this->create_device_record();
 113  
 114          $devicekeypair = sodium_crypto_box_keypair();
 115          $publickey = sodium_bin2base64(
 116              sodium_crypto_box_publickey($devicekeypair),
 117              SODIUM_BASE64_VARIANT_ORIGINAL
 118          );
 119  
 120          // Invalid appid.
 121          $result = update_user_device_public_key::execute(
 122              'invaliduuid',
 123              $device->appid,
 124              $publickey,
 125          );
 126  
 127          $result = external_api::clean_returnvalue(update_user_device_public_key::execute_returns(), $result);
 128          $this->assertFalse($result['status']);
 129          $this->assertNotEmpty($result['warnings']);
 130      }
 131  }