/files/tests/externallib_test.php

https://github.com/moodlehq/totara · PHP · 127 lines · 64 code · 22 blank · 41 comment · 1 complexity · 5c404ce13e381377afd1bebd3d990e7e MD5 · raw file

  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. * PHPunit tests for external files API.
  18. *
  19. * @package core_files
  20. * @category external
  21. * @copyright 2013 Ankit Agarwal
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. * @since Moodle 2.6
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. global $CFG;
  27. require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  28. require_once($CFG->dirroot . '/files/externallib.php');
  29. class test_external_files extends advanced_testcase {
  30. /*
  31. * Test core_files_external::upload().
  32. */
  33. public function test_upload() {
  34. global $USER;
  35. $this->resetAfterTest();
  36. $this->setAdminUser();
  37. $context = context_user::instance($USER->id);
  38. $contextid = $context->id;
  39. $component = "user";
  40. $filearea = "private";
  41. $itemid = 0;
  42. $filepath = "/";
  43. $filename = "Simple.txt";
  44. $filecontent = base64_encode("Let us create a nice simple file");
  45. $browser = get_file_browser();
  46. // Make sure no file exists.
  47. $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
  48. $this->assertEmpty($file);
  49. // Call the api to create a file.
  50. core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
  51. // Make sure the file was created.
  52. $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
  53. $this->assertNotEmpty($file);
  54. // Make sure no file exists.
  55. $itemid = 2;
  56. $filename = "Simple2.txt";
  57. $file = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename);
  58. $this->assertEmpty($file);
  59. // Call the api to create a file.
  60. $fileinfo = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
  61. // Make sure itemid is always set to 0.
  62. $this->assertEquals(0, $fileinfo['itemid']);
  63. // Make sure the same file cannot be created again.
  64. $this->setExpectedException("moodle_exception");
  65. core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
  66. }
  67. /*
  68. * Make sure only user component is allowed in core_files_external::upload().
  69. */
  70. public function test_upload_param_component() {
  71. global $USER;
  72. $this->resetAfterTest();
  73. $this->setAdminUser();
  74. $context = context_user::instance($USER->id);
  75. $contextid = $context->id;
  76. $component = "backup";
  77. $filearea = "private";
  78. $itemid = 0;
  79. $filepath = "/";
  80. $filename = "Simple3.txt";
  81. $filecontent = base64_encode("Let us create a nice simple file");
  82. // Make sure exception is thrown.
  83. $this->setExpectedException("coding_exception");
  84. core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
  85. }
  86. /*
  87. * Make sure only private area is allowed in core_files_external::upload().
  88. */
  89. public function test_upload_param_area() {
  90. global $USER;
  91. $this->resetAfterTest();
  92. $this->setAdminUser();
  93. $context = context_user::instance($USER->id);
  94. $contextid = $context->id;
  95. $component = "user";
  96. $filearea = "draft";
  97. $itemid = 0;
  98. $filepath = "/";
  99. $filename = "Simple4.txt";
  100. $filecontent = base64_encode("Let us create a nice simple file");
  101. // Make sure exception is thrown.
  102. $this->setExpectedException("coding_exception");
  103. core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath, $filename, $filecontent);
  104. }
  105. }