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 310 and 401] [Versions 39 and 401] [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 repository_nextcloud;
  18  
  19  use testable_access_controlled_link_manager;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  require_once($CFG->libdir . '/webdavlib.php');
  25  require_once($CFG->dirroot . '/repository/nextcloud/tests/fixtures/testable_access_controlled_link_manager.php');
  26  
  27  /**
  28   * Class repository_nextcloud_testcase
  29   *
  30   * @package repository_nextcloud
  31   * @group repository_nextcloud
  32   * @copyright  2017 Project seminar (Learnweb, University of Münster)
  33   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class access_controlled_link_manager_test extends \advanced_testcase {
  36  
  37      /** @var null|testable_access_controlled_link_manager a malleable variant of the access_controlled_link_manager. */
  38      public $linkmanager = null;
  39  
  40      /** @var null|\repository_nextcloud\ocs_client The ocs_client used to send requests. */
  41      public $ocsmockclient = null;
  42  
  43      /** @var null|\core\oauth2\client Mock oauth client for the system account. */
  44      private $oauthsystemmock = null;
  45  
  46      /** @var null|\core\oauth2\issuer which belongs to the repository_nextcloud object. */
  47      public $issuer = null;
  48  
  49      /**
  50       * SetUp to create an repository instance.
  51       */
  52      protected function setUp(): void {
  53          $this->resetAfterTest(true);
  54  
  55          // Admin is necessary to create issuer object.
  56          $this->setAdminUser();
  57  
  58          $generator = $this->getDataGenerator()->get_plugin_generator('repository_nextcloud');
  59          $this->issuer = $generator->test_create_issuer();
  60          $generator->test_create_endpoints($this->issuer->get('id'));
  61  
  62          // Mock clients.
  63          $this->ocsmockclient = $this->getMockBuilder(ocs_client::class
  64          )->disableOriginalConstructor()->disableOriginalClone()->getMock();
  65          $this->oauthsystemmock = $this->getMockBuilder(\core\oauth2\client::class
  66          )->disableOriginalConstructor()->disableOriginalClone()->getMock();
  67          $systemwebdavclient = $this->getMockBuilder(\webdav_client::class
  68          )->disableOriginalConstructor()->disableOriginalClone()->getMock();
  69          $systemocsclient = $systemocsclient = $this->getMockBuilder(ocs_client::class
  70          )->disableOriginalConstructor()->disableOriginalClone()->getMock();
  71  
  72          // Pseudo system account user.
  73          $this->systemaccountusername = 'pseudouser';
  74          $record = new \stdClass();
  75          $record->issuerid = $this->issuer->get('id');
  76          $record->refreshtoken = 'pseudotoken';
  77          $record->grantedscopes = 'scopes';
  78          $record->email = '';
  79          $record->username = $this->systemaccountusername;
  80          $systemaccount = new \core\oauth2\system_account(0, $record);
  81          $systemaccount->create();
  82  
  83          $this->linkmanager = new testable_access_controlled_link_manager($this->ocsmockclient,
  84              $this->oauthsystemmock, $systemocsclient,
  85              $this->issuer, 'Nextcloud', $systemwebdavclient);
  86  
  87      }
  88  
  89      /**
  90       * Function to test the private function create_share_user_sysaccount.
  91       */
  92      public function test_create_share_user_sysaccount_user_shares() {
  93          $params = [
  94              'path' => "/ambient.txt",
  95              'shareType' => \repository_nextcloud\ocs_client::SHARE_TYPE_USER,
  96              'publicUpload' => false,
  97              'shareWith' => $this->systemaccountusername,
  98              'permissions' => \repository_nextcloud\ocs_client::SHARE_PERMISSION_READ,
  99          ];
 100          $expectedresponse = <<<XML
 101  <?xml version="1.0"?>
 102  <ocs>
 103   <meta>
 104    <status>ok</status>
 105    <statuscode>100</statuscode>
 106    <message/>
 107   </meta>
 108   <data>
 109    <id>207</id>
 110    <share_type>0</share_type>
 111    <uid_owner>user1</uid_owner>
 112    <displayname_owner>user1</displayname_owner>
 113    <permissions>19</permissions>
 114    <stime>1511532198</stime>
 115    <parent/>
 116    <expiration/>
 117    <token/>
 118    <uid_file_owner>user1</uid_file_owner>
 119    <displayname_file_owner>user1</displayname_file_owner>
 120    <path>/ambient.txt</path>
 121    <item_type>file</item_type>
 122    <mimetype>text/plain</mimetype>
 123    <storage_id>home::user1</storage_id>
 124    <storage>3</storage>
 125    <item_source>545</item_source>
 126    <file_source>545</file_source>
 127    <file_parent>20</file_parent>
 128    <file_target>/ambient.txt</file_target>
 129    <share_with>tech</share_with>
 130    <share_with_displayname>tech</share_with_displayname>
 131    <mail_send>0</mail_send>
 132   </data>
 133  </ocs>
 134  XML;
 135          $this->ocsmockclient->expects($this->once())->method('call')->with('create_share', $params)->will(
 136              $this->returnValue($expectedresponse));
 137  
 138          $result = $this->linkmanager->create_share_user_sysaccount("/ambient.txt");
 139          $xml = simplexml_load_string($expectedresponse);
 140          $expected = array();
 141          $expected['statuscode'] = (int)$xml->meta->statuscode;
 142          $expected['shareid'] = (int)$xml->data->id;
 143          $expected['filetarget'] = (string)$xml->data[0]->file_target;
 144          $this->assertEquals($expected, $result);
 145      }
 146      /**
 147       * Test the delete_share_function. In case the request fails, the function throws an exception, however this
 148       * can not be tested in phpUnit since it is javascript.
 149       */
 150      public function test_delete_share_dataowner_sysaccount() {
 151          $shareid = 5;
 152          $deleteshareparams = [
 153              'share_id' => $shareid
 154          ];
 155          $returnxml = <<<XML
 156  <?xml version="1.0"?>
 157  <ocs>
 158      <meta>
 159      <status>ok</status>
 160      <statuscode>100</statuscode>
 161      <message/>
 162      </meta>
 163      <data/>
 164  </ocs>
 165  XML;
 166          $this->ocsmockclient->expects($this->once())->method('call')->with('delete_share', $deleteshareparams)->will(
 167              $this->returnValue($returnxml));
 168          $this->linkmanager->delete_share_dataowner_sysaccount($shareid, 'repository_nextcloud');
 169  
 170      }
 171  
 172      /**
 173       * Function which test that create folder path does return the adequate results (path and success).
 174       * Additionally mock checks whether the right params are passed to the corresponding functions.
 175       */
 176      public function test_create_folder_path_folders_are_not_created() {
 177  
 178          $mocks = $this->set_up_mocks_for_create_folder_path(true, 'somename');
 179          $this->set_private_property($mocks['mockclient'], 'systemwebdavclient', $this->linkmanager);
 180          $result = $this->linkmanager->create_folder_path_access_controlled_links($mocks['mockcontext'], "mod_resource",
 181              'content', 0);
 182          $this->assertEquals('/somename (ctx )/mod_resource/content/0', $result);
 183      }
 184      /**
 185       * Function which test that create folder path does return the adequate results (path and success).
 186       * Additionally mock checks whether the right params are passed to the corresponding functions.
 187       */
 188      public function test_create_folder_path_folders_are_created() {
 189  
 190          // In Context is okay, number of context counts for number of iterations.
 191          $mocks = $this->set_up_mocks_for_create_folder_path(false, 'somename/withslash', true, 201);
 192          $this->set_private_property($mocks['mockclient'], 'systemwebdavclient', $this->linkmanager);
 193          $result = $this->linkmanager->create_folder_path_access_controlled_links($mocks['mockcontext'], "mod_resource",
 194              'content', 0);
 195          $this->assertEquals('/somenamewithslash (ctx )/mod_resource/content/0', $result);
 196      }
 197      /**
 198       * Test whether the create_folder_path methode throws exception.
 199       */
 200      public function test_create_folder_path_folder_creation_fails() {
 201  
 202          $mocks = $this->set_up_mocks_for_create_folder_path(false, 'somename', true, 400);
 203          $this->set_private_property($mocks['mockclient'], 'systemwebdavclient', $this->linkmanager);
 204          $this->expectException(\repository_nextcloud\request_exception::class);
 205          $this->linkmanager->create_folder_path_access_controlled_links($mocks['mockcontext'], "mod_resource",
 206              'content', 0);
 207      }
 208  
 209      /**
 210       * Helper function to generate mocks for testing create folder path.
 211       * @param bool $returnisdir Return value mocking the result of invoking is_dir
 212       * @param bool $returnestedcontext Name of the folder that is simulated to be checked/created
 213       * @param bool $callmkcol Also mock creation of the folder
 214       * @param int $returnmkcol Return value mocking the result of invoking mkcol
 215       * @return array ['mockcontext' context_module mock, 'mockclient' => webdav client mock]
 216       */
 217      protected function set_up_mocks_for_create_folder_path($returnisdir, $returnestedcontext, $callmkcol = false,
 218                                                             $returnmkcol = 201) {
 219          $mockcontext = $this->createMock(\context_module::class);
 220          $mockclient = $this->getMockBuilder(\webdav_client::class
 221          )->disableOriginalConstructor()->disableOriginalClone()->getMock();
 222          $parsedwebdavurl = parse_url($this->issuer->get_endpoint_url('webdav'));
 223          $webdavprefix = $parsedwebdavurl['path'];
 224          // Empty ctx 'id' expected because using code will not be able to access $ctx->id.
 225          $cleanedcontextname = clean_param($returnestedcontext, PARAM_FILE);
 226          $dirstring = $webdavprefix . '/' . $cleanedcontextname . ' (ctx )';
 227          $mockclient->expects($this->atMost(4))->method('is_dir')->with($this->logicalOr(
 228              $dirstring, $dirstring . '/mod_resource', $dirstring . '/mod_resource/content',
 229              $dirstring . '/mod_resource/content/0'))->willReturn($returnisdir);
 230          if ($callmkcol == true) {
 231              $mockclient->expects($this->atMost(4))->method('mkcol')->willReturn($returnmkcol);
 232          }
 233          $mockcontext->method('get_parent_contexts')->willReturn(array('1' => $mockcontext));
 234          $mockcontext->method('get_context_name')->willReturn($returnestedcontext);
 235  
 236          return array('mockcontext' => $mockcontext, 'mockclient' => $mockclient);
 237      }
 238  
 239      /**
 240       * Test whether the right methods from the webdavclient are called when the storage_folder is created.
 241       * 1. Directory already exist -> no further action needed.
 242       */
 243      public function test_create_storage_folder_success() {
 244          $mockwebdavclient = $this->createMock(\webdav_client::class);
 245          $url = $this->issuer->get_endpoint_url('webdav');
 246          $parsedwebdavurl = parse_url($url);
 247          $webdavprefix = $parsedwebdavurl['path'];
 248          $mockwebdavclient->expects($this->once())->method('open')->willReturn(true);
 249          $mockwebdavclient->expects($this->once())->method('is_dir')->with($webdavprefix . 'myname')->willReturn(true);
 250          $mockwebdavclient->expects($this->once())->method('close');
 251          $this->linkmanager->create_storage_folder('myname', $mockwebdavclient);
 252  
 253      }
 254      /**
 255       * Test whether the right methods from the webdavclient are called when the storage_folder is created.
 256       * 2. Directory does not exist. It is created with mkcol and returns a success.
 257       *
 258       */
 259      public function test_create_storage_folder_success_mkcol() {
 260          $mockwebdavclient = $this->createMock(\webdav_client::class);
 261          $url = $this->issuer->get_endpoint_url('webdav');
 262          $parsedwebdavurl = parse_url($url);
 263          $webdavprefix = $parsedwebdavurl['path'];
 264          $mockwebdavclient->expects($this->once())->method('open')->willReturn(true);
 265          $mockwebdavclient->expects($this->once())->method('is_dir')->with($webdavprefix . 'myname')->willReturn(false);
 266          $mockwebdavclient->expects($this->once())->method('mkcol')->with($webdavprefix . 'myname')->willReturn(201);
 267          $mockwebdavclient->expects($this->once())->method('close');
 268  
 269          $this->linkmanager->create_storage_folder('myname', $mockwebdavclient);
 270      }
 271      /**
 272       * Test whether the right methods from the webdavclient are called when the storage_folder is created.
 273       * 3. Request to create Folder fails.
 274       */
 275      public function test_create_storage_folder_failure() {
 276          $mockwebdavclient = $this->createMock(\webdav_client::class);
 277          $url = $this->issuer->get_endpoint_url('webdav');
 278          $parsedwebdavurl = parse_url($url);
 279          $webdavprefix = $parsedwebdavurl['path'];
 280          $mockwebdavclient->expects($this->once())->method('open')->willReturn(true);
 281          $mockwebdavclient->expects($this->once())->method('is_dir')->with($webdavprefix . 'myname')->willReturn(false);
 282          $mockwebdavclient->expects($this->once())->method('mkcol')->with($webdavprefix . 'myname')->willReturn(400);
 283  
 284          $this->expectException(\repository_nextcloud\request_exception::class);
 285          $this->linkmanager->create_storage_folder('myname', $mockwebdavclient);
 286      }
 287      /**
 288       * Test whether the webdav client gets the right params and whether function differentiates between move and copy.
 289       */
 290      public function test_transfer_file_to_path_copyfile() {
 291          // Initialize params.
 292          $parsedwebdavurl = parse_url($this->issuer->get_endpoint_url('webdav'));
 293          $webdavprefix = $parsedwebdavurl['path'];
 294          $srcpath = 'sourcepath';
 295          $dstpath = "destinationpath/another/path";
 296  
 297          // Mock the Webdavclient and set expected methods.
 298          $systemwebdavclientmock = $this->createMock(\webdav_client::class);
 299          $systemwebdavclientmock->expects($this->once())->method('open')->willReturn(true);
 300          $systemwebdavclientmock->expects($this->once())->method('copy_file')->with($webdavprefix . $srcpath,
 301              $webdavprefix . $dstpath . '/' . $srcpath, true)->willReturn(201);
 302          $this->set_private_property($systemwebdavclientmock, 'systemwebdavclient', $this->linkmanager);
 303  
 304          // Call of function.
 305          $result = $this->linkmanager->transfer_file_to_path($srcpath, $dstpath, 'copy');
 306  
 307          $this->assertEquals(201, $result);
 308      }
 309      /**
 310       * Test whether the webdav client gets the right params and whether function handles overwrite.
 311       *
 312       * @covers \repository_nextcloud\access_controlled_link_manager::transfer_file_to_path
 313       */
 314      public function test_transfer_file_to_path_overwritefile() {
 315          // Initialize params.
 316          $parsedwebdavurl = parse_url($this->issuer->get_endpoint_url('webdav'));
 317          $webdavprefix = $parsedwebdavurl['path'];
 318          $srcpath = 'sourcepath';
 319          $dstpath = "destinationpath/another/path";
 320  
 321          // Mock the Webdavclient and set expected methods.
 322          $systemwebdavclientmock = $this->createMock(\webdav_client::class);
 323          $systemwebdavclientmock->expects($this->once())->method('open')->willReturn(true);
 324          $systemwebdavclientmock->expects($this->once())->method('copy_file')->with($webdavprefix . $srcpath,
 325              $webdavprefix . $dstpath . '/' . $srcpath, true)->willReturn(204);
 326          $this->set_private_property($systemwebdavclientmock, 'systemwebdavclient', $this->linkmanager);
 327  
 328          // Call of function.
 329          $result = $this->linkmanager->transfer_file_to_path($srcpath, $dstpath, 'copy');
 330  
 331          $this->assertEquals(204, $result);
 332      }
 333      /**
 334       * This function tests whether the function transfer_file_to_path() moves or copies a given file to a given path
 335       * It tests whether the webdav_client gets the right parameter and whether function distinguishes between move and copy.
 336       *
 337       */
 338      public function test_transfer_file_to_path_copyfile_movefile() {
 339          // Initialize params.
 340          $parsedwebdavurl = parse_url($this->issuer->get_endpoint_url('webdav'));
 341          $webdavprefix = $parsedwebdavurl['path'];
 342          $srcpath = 'sourcepath';
 343          $dstpath = "destinationpath/another/path";
 344  
 345          $systemwebdavclientmock = $this->createMock(\webdav_client::class);
 346  
 347          $systemwebdavclientmock->expects($this->once())->method('open')->willReturn(true);
 348          $this->set_private_property($systemwebdavclientmock, 'systemwebdavclient', $this->linkmanager);
 349          $webdavclientmock = $this->createMock(\webdav_client::class);
 350  
 351          $webdavclientmock->expects($this->once())->method('move')->with($webdavprefix . $srcpath,
 352              $webdavprefix . $dstpath . '/' . $srcpath, false)->willReturn(201);
 353          $result = $this->linkmanager->transfer_file_to_path($srcpath, $dstpath, 'move', $webdavclientmock);
 354          $this->assertEquals(201, $result);
 355      }
 356  
 357      /**
 358       * Test the get_shares_from path() function. This function extracts from an list of shares the share of a given user
 359       * (the username is a parameter in the function call) and returns the id. The test firstly test whether the right fileid
 360       * for user1 is extracted then for user2 and last but least whether an error is thrown if the user does not have a share.
 361       * @throws moodle_exception
 362       */
 363      public function test_get_shares_from_path() {
 364          $params = [
 365              'path' => '/Kernsystem/Kursbereich Miscellaneous/Kurs Example Course/Datei zet/mod_resource/content/0/picture.png',
 366              'reshares' => true
 367          ];
 368          $reference = new \stdClass();
 369          $reference->link = "/Kernsystem/Kursbereich Miscellaneous/Kurs Example Course/Datei zet/mod_resource/content/0/picture.png";
 370          $reference->name = "f\u00fcrdennis.png";
 371          $reference->usesystem = true;
 372          $expectedresponse = <<<XML
 373  <?xml version="1.0"?>
 374  <ocs>
 375   <meta>
 376    <status>ok</status>
 377    <statuscode>100</statuscode>
 378    <message/>
 379   </meta>
 380   <data>
 381    <element>
 382     <id>292</id>
 383     <share_type>0</share_type>
 384     <uid_owner>tech</uid_owner>
 385     <displayname_owner>tech</displayname_owner>
 386     <permissions>19</permissions>
 387     <stime>1515752494</stime>
 388     <parent/>
 389     <expiration/>
 390     <token/>
 391     <uid_file_owner>tech</uid_file_owner>
 392     <displayname_file_owner>tech</displayname_file_owner>
 393     <path>some/path/of/some/file.pdf</path>
 394     <item_type>file</item_type>
 395     <mimetype>image/png</mimetype>
 396     <storage_id>home::tech</storage_id>
 397     <storage>4</storage>
 398     <item_source>1085</item_source>
 399     <file_source>1085</file_source>
 400     <file_parent>1084</file_parent>
 401     <file_target>/fehler (3).png</file_target>
 402     <share_with>user1</share_with>
 403     <share_with_displayname>user1</share_with_displayname>
 404     <mail_send>0</mail_send>
 405    </element>
 406    <element>
 407     <id>293</id>
 408     <share_type>0</share_type>
 409     <uid_owner>tech</uid_owner>
 410     <displayname_owner>tech</displayname_owner>
 411     <permissions>19</permissions>
 412     <stime>1515752494</stime>
 413     <parent/>
 414     <expiration/>
 415     <token/>
 416     <uid_file_owner>tech</uid_file_owner>
 417     <displayname_file_owner>tech</displayname_file_owner>
 418     <path>some/path/of/some/file.pdf</path>
 419     <item_type>file</item_type>
 420     <mimetype>image/png</mimetype>
 421     <storage_id>home::tech</storage_id>
 422     <storage>4</storage>
 423     <item_source>1085</item_source>
 424     <file_source>1085</file_source>
 425     <file_parent>1084</file_parent>
 426     <file_target>/fehler (3).png</file_target>
 427     <share_with>user2</share_with>
 428     <share_with_displayname>user2</share_with_displayname>
 429     <mail_send>0</mail_send>
 430    </element>
 431   </data>
 432  </ocs>
 433  XML;
 434          $this->set_private_property($this->ocsmockclient, 'systemocsclient', $this->linkmanager);
 435  
 436          $this->ocsmockclient->expects($this->exactly(3))->method('call')->with('get_shares', $params)->will(
 437              $this->returnValue($expectedresponse));
 438          $xmlobjuser1 = (int) $this->linkmanager->get_shares_from_path($reference->link, 'user2');
 439          $xmlobjuser2 = (int) $this->linkmanager->get_shares_from_path($reference->link, 'user1');
 440  
 441          $this->assertEquals(293, $xmlobjuser1);
 442          $this->assertEquals(292, $xmlobjuser2);
 443  
 444          $this->expectException(\repository_nextcloud\request_exception::class);
 445  
 446          $this->expectExceptionMessage('A request to Nextcloud has failed. The requested file could not be accessed. Please ' .
 447              'check whether you have chosen a valid file and you are authenticated with the right account.');
 448          $this->linkmanager->get_shares_from_path($reference->link, 'user3');
 449  
 450      }
 451      /** Test whether the systemwebdav client is constructed correctly. Port is set to 443 in case of https, to 80 in
 452       * case of http and exception is thrown when endpoint does not exist.
 453       * @throws \repository_nextcloud\configuration_exception
 454       * @throws coding_exception
 455       */
 456      public function test_create_system_dav() {
 457          // Initialize mock and params.
 458          $fakeaccesstoken = new \stdClass();
 459          $fakeaccesstoken->token = "fake access token";
 460          // Use `atLeastOnce` instead of `exactly(2)` because it is only called a second time on dev systems that allow http://.
 461          $this->oauthsystemmock->expects($this->atLeastOnce())->method('get_accesstoken')->willReturn($fakeaccesstoken);
 462          $parsedwebdavurl = parse_url($this->issuer->get_endpoint_url('webdav'));
 463  
 464          // Call function and create own client.
 465          $dav = $this->linkmanager->create_system_dav();
 466          $mydav = new \webdav_client($parsedwebdavurl['host'], '', '', 'bearer', 'ssl://',
 467              "fake access token", $parsedwebdavurl['path']);
 468          $mydav->port = 443;
 469          $mydav->debug = false;
 470          $this->assertEquals($mydav, $dav);
 471  
 472          // Deletes the old webdav endpoint and ...
 473          $this->delete_endpoints('webdav_endpoint');
 474          // Creates a new one which requires different ports.
 475          try {
 476              $endpoint = new \stdClass();
 477              $endpoint->name = "webdav_endpoint";
 478              $endpoint->url = 'http://www.default.test/webdav/index.php';
 479              $endpoint->issuerid = $this->issuer->get('id');
 480              \core\oauth2\api::create_endpoint($endpoint);
 481  
 482              // Call function and create own client.
 483              $dav = $this->linkmanager->create_system_dav();
 484              $mydav = new \webdav_client($parsedwebdavurl['host'], '', '', 'bearer', '',
 485                  "fake access token");
 486              $mydav->port = 80;
 487              $mydav->debug = false;
 488              $this->assertEquals($mydav, $dav);
 489          } catch (core\invalid_persistent_exception $e) {
 490              // In some cases Moodle does not allow to create http connections. In those cases the exception
 491              // is catched here and the test are executed.
 492              $this->expectException(\core\invalid_persistent_exception::class);
 493              $this->linkmanager->create_system_dav();
 494          } finally {
 495  
 496              // Delte endpoints and ...
 497              $this->delete_endpoints('webdav_endpoint');
 498  
 499              // Do not insert new ones, therefore exception is thrown.
 500              $this->expectException(\repository_nextcloud\configuration_exception::class);
 501              $this->linkmanager->create_system_dav();
 502          }
 503      }
 504  
 505      /**
 506       * Tests the function get_share_information_from_shareid(). From a response with two element it is tested
 507       * whether the right file_target is extracted and lastly it is checked whether an error is thrown in case no suitable
 508       * element exists.
 509       * @throws \repository_nextcloud\request_exception
 510       * @throws coding_exception
 511       */
 512      public function test_get_share_information_from_shareid() {
 513          $params303 = [
 514              'share_id' => 303,
 515          ];
 516          $params302 = [
 517              'share_id' => 302,
 518          ];
 519          $expectedresponse = <<<XML
 520  <?xml version="1.0"?>
 521  <ocs>
 522   <meta>
 523    <status>ok</status>
 524    <statuscode>100</statuscode>
 525    <message/>
 526   </meta>
 527   <data>
 528    <element>
 529     <id>302</id>
 530     <share_type>0</share_type>
 531     <uid_owner>tech</uid_owner>
 532     <displayname_owner>tech</displayname_owner>
 533     <permissions>19</permissions>
 534     <stime>1516096325</stime>
 535     <parent/>
 536     <expiration/>
 537     <token/>
 538     <uid_file_owner>tech</uid_file_owner>
 539     <displayname_file_owner>tech</displayname_file_owner>
 540       <path>/some/target (2).png</path>
 541     <item_type>file</item_type>
 542     <mimetype>image/png</mimetype>
 543     <storage_id>shared::/some/target.png</storage_id>
 544     <storage>4</storage>
 545     <item_source>1125</item_source>
 546     <file_source>1125</file_source>
 547     <file_parent>20</file_parent>
 548     <file_target>/some/target.png</file_target>
 549     <share_with>user1</share_with>
 550     <share_with_displayname>user1</share_with_displayname>
 551     <mail_send>0</mail_send>
 552    </element>
 553    <element>
 554     <id>303</id>
 555     <share_type>0</share_type>
 556     <uid_owner>tech</uid_owner>
 557     <displayname_owner>tech</displayname_owner>
 558     <permissions>19</permissions>
 559     <stime>1516096325</stime>
 560     <parent/>
 561     <expiration/>
 562     <token/>
 563     <uid_file_owner>tech</uid_file_owner>
 564     <displayname_file_owner>tech</displayname_file_owner>
 565     <path>/some/target (2).pdf</path>
 566     <item_type>file</item_type>
 567     <mimetype>image/png</mimetype>
 568     <storage_id>shared::/some/target.pdf</storage_id>
 569     <storage>4</storage>
 570     <item_source>1125</item_source>
 571     <file_source>1125</file_source>
 572     <file_parent>20</file_parent>
 573     <file_target>/some/target.pdf</file_target>
 574     <share_with>user2</share_with>
 575     <share_with_displayname>user1</share_with_displayname>
 576     <mail_send>0</mail_send>
 577    </element>
 578   </data>
 579  </ocs>
 580  XML;
 581          $this->set_private_property($this->ocsmockclient, 'systemocsclient', $this->linkmanager);
 582  
 583          $this->ocsmockclient->expects($this->exactly(3))->method('call')->with('get_information_of_share',
 584              $this->logicalOr($params303, $params302))->will($this->returnValue($expectedresponse));
 585  
 586          // Test function for two different users. Setting the id is just a dummy value since always $expectedresponse ...
 587          // ... is returned.
 588          $filetarget = $this->linkmanager->get_share_information_from_shareid(303, 'user2');
 589          $this->assertEquals('/some/target.pdf', $filetarget);
 590  
 591          $filetarget = $this->linkmanager->get_share_information_from_shareid(302, 'user1');
 592          $this->assertEquals('/some/target.png', $filetarget);
 593  
 594          // Expect exception in case no suitable elemtn exist in the response.
 595          $this->expectException(\repository_nextcloud\request_exception::class);
 596          $this->expectExceptionMessage('A request to Nextcloud has failed. The requested file could not be accessed. Please ' .
 597              'check whether you have chosen a valid file and you are authenticated with the right account.');
 598          $this->linkmanager->get_share_information_from_shareid(302, 'user3');
 599      }
 600  
 601      /**
 602       * Helper method which inserts a value into a non-public field of an object.
 603       *
 604       * @param mixed $value mock value that will be inserted.
 605       * @param string $propertyname name of the private property.
 606       * @param object $class Instance that is being modified.
 607       * @return ReflectionProperty the resulting reflection property.
 608       */
 609      protected function set_private_property($value, $propertyname, $class) {
 610          $refclient = new \ReflectionClass($class);
 611          $private = $refclient->getProperty($propertyname);
 612          $private->setAccessible(true);
 613          $private->setValue($class, $value);
 614          return $private;
 615      }
 616      /**
 617       * Helper method which gets a value from a non-public field of an object.
 618       *
 619       * @param string $propertyname name of the private property.
 620       * @param object $class Instance that is being modified.
 621       * @return mixed the resulting value.
 622       */
 623      protected function get_private_property($propertyname, $class) {
 624          $refclient = new \ReflectionClass($class);
 625          $private = $refclient->getProperty($propertyname);
 626          $private->setAccessible(true);
 627          $property = $private->getValue($private);
 628          return $property;
 629      }
 630      /**
 631       * Deletes all endpoint with the given name.
 632       * @param string $endpointname
 633       * @return array|null
 634       * @throws moodle_exception
 635       */
 636      protected function delete_endpoints($endpointname) {
 637          $endpoints = \core\oauth2\api::get_endpoints($this->issuer);
 638          $arrayofids = array();
 639          foreach ($endpoints as $endpoint) {
 640              $name = $endpoint->get('name');
 641              if ($name === $endpointname) {
 642                  $arrayofids[$endpoint->get('id')] = $endpoint->get('id');
 643              }
 644          }
 645          if (empty($arrayofids)) {
 646              return;
 647          }
 648          foreach ($arrayofids as $id) {
 649              \core\oauth2\api::delete_endpoint($id);
 650          }
 651      }
 652  
 653  }