PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/repository/googledocs/tests/helper_test.php

https://bitbucket.org/moodle/moodle
PHP | 281 lines | 160 code | 27 blank | 94 comment | 1 complexity | 6c7f56fa579702b9a544a0b2a7d7380e MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. namespace repository_googledocs;
  17. defined('MOODLE_INTERNAL') || die();
  18. global $CFG;
  19. require_once($CFG->dirroot . '/repository/googledocs/tests/repository_googledocs_testcase.php');
  20. require_once($CFG->dirroot . '/repository/googledocs/lib.php');
  21. /**
  22. * Class containing unit tests for the helper class.
  23. *
  24. * @package repository_googledocs
  25. * @copyright 2021 Mihail Geshoski <mihail@moodle.com>
  26. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27. */
  28. class helper_test extends \repository_googledocs_testcase {
  29. /**
  30. * Test build_node_path().
  31. *
  32. * @dataProvider build_node_path_provider
  33. * @param string $id The ID of the node
  34. * @param string $name The name of the node
  35. * @param string $rootpath The path to append the node on
  36. * @param string $expected The expected node path
  37. */
  38. public function test_build_node_path(string $id, string $name, string $rootpath, string $expected) {
  39. // Assert that the returned node path is equal to the expected one.
  40. $this->assertEquals($expected, helper::build_node_path($id, $name, $rootpath));
  41. }
  42. /**
  43. * Data provider for test_build_node_path().
  44. *
  45. * @return array
  46. */
  47. public function build_node_path_provider(): array {
  48. $rootid = \repository_googledocs::REPOSITORY_ROOT_ID;
  49. $mydriveid = \repository_googledocs::MY_DRIVE_ROOT_ID;
  50. $shareddrivesid = \repository_googledocs::SHARED_DRIVES_ROOT_ID;
  51. return [
  52. 'Generate the path for a node without a root path.' =>
  53. [
  54. $rootid,
  55. 'Google Drive',
  56. '',
  57. "{$rootid}|Google+Drive",
  58. ],
  59. 'Generate the path for a node without a name and root path.' =>
  60. [
  61. $rootid,
  62. '',
  63. '',
  64. $rootid,
  65. ],
  66. 'Generate the path for a node without a name.' =>
  67. [
  68. $mydriveid,
  69. '',
  70. "{$rootid}|Google+Drive",
  71. "{$rootid}|Google+Drive/{$mydriveid}",
  72. ],
  73. 'Generate the path for a node which has a name and root path.' =>
  74. [
  75. '092cdf4732b9d5',
  76. 'Shared Drive 5',
  77. "{$rootid}|Google+Drive/{$shareddrivesid}|Shared+Drives",
  78. "{$rootid}|Google+Drive/{$shareddrivesid}|Shared+Drives/092cdf4732b9d5|Shared+Drive+5",
  79. ],
  80. ];
  81. }
  82. /**
  83. * Test explode_node_path().
  84. *
  85. * @dataProvider explode_node_path_provider
  86. * @param string $node The node string to extract information from
  87. * @param array $expected The expected array containing the information about the node
  88. */
  89. public function test_explode_node_path(string $node, array $expected) {
  90. // Assert that the returned array is equal to the expected one.
  91. $this->assertEquals($expected, helper::explode_node_path($node));
  92. }
  93. /**
  94. * Data provider for test_explode_node_path().
  95. *
  96. * @return array
  97. */
  98. public function explode_node_path_provider(): array {
  99. $rootid = \repository_googledocs::REPOSITORY_ROOT_ID;
  100. return [
  101. 'Return the information for a path node that has a name.' =>
  102. [
  103. "{$rootid}|Google+Drive",
  104. [
  105. 0 => $rootid,
  106. 1 => 'Google Drive',
  107. 'id' => $rootid,
  108. 'name' => 'Google Drive',
  109. ],
  110. ],
  111. 'Return the information for a path node that does not have a name.' =>
  112. [
  113. $rootid,
  114. [
  115. 0 => $rootid,
  116. 1 => '',
  117. 'id' => $rootid,
  118. 'name' => '',
  119. ],
  120. ],
  121. ];
  122. }
  123. /**
  124. * Test get_browser().
  125. *
  126. * @dataProvider get_browser_provider
  127. * @param string $nodepath The node path string
  128. * @param string $expected The expected browser class
  129. */
  130. public function test_get_browser(string $nodepath, string $expected) {
  131. // The service (rest API) object is required by get_browser(), but not being used to determine which browser
  132. // object should be returned. Therefore, we can simply mock this object in this test.
  133. $servicemock = $this->createMock(rest::class);
  134. $browser = helper::get_browser($servicemock, $nodepath);
  135. // Assert that the returned browser class by get_browser() is equal to the expected one.
  136. $this->assertEquals($expected, get_class($browser));
  137. }
  138. /**
  139. * Data provider for test_get_browser().
  140. *
  141. * @return array
  142. */
  143. public function get_browser_provider(): array {
  144. $rootid = \repository_googledocs::REPOSITORY_ROOT_ID;
  145. $mydriveid = \repository_googledocs::MY_DRIVE_ROOT_ID;
  146. $shareddrivesid = \repository_googledocs::SHARED_DRIVES_ROOT_ID;
  147. return [
  148. 'Repository root level path.' =>
  149. [
  150. "{$rootid}|Google+Drive",
  151. \repository_googledocs\local\browser\googledocs_root_content::class,
  152. ],
  153. 'My drive path.' =>
  154. [
  155. "{$rootid}|Google+Drive/{$mydriveid}|My+Drive",
  156. \repository_googledocs\local\browser\googledocs_drive_content::class,
  157. ],
  158. 'Shared drives root path.' =>
  159. [
  160. "{$rootid}|Google+Drive/{$shareddrivesid}|Shared+Drives",
  161. \repository_googledocs\local\browser\googledocs_shared_drives_content::class,
  162. ],
  163. 'Path within a shared drive.' =>
  164. [
  165. "{$rootid}|Google+Drive/{$shareddrivesid}|Shared+Drives/092cdf4732b9d5|Shared+Drive+5",
  166. \repository_googledocs\local\browser\googledocs_drive_content::class,
  167. ],
  168. ];
  169. }
  170. /**
  171. * Test get_node().
  172. *
  173. * @dataProvider get_node_provider
  174. * @param \stdClass $gdcontent The Google Drive content (file/folder) object
  175. * @param string $expected The expected content node class
  176. */
  177. public function test_get_node(\stdClass $gdcontent, string $expected) {
  178. // The path is required by get_content_node(), but not being used to determine which content node
  179. // object should be returned. Therefore, we can just generate a dummy path.
  180. $path = \repository_googledocs::REPOSITORY_ROOT_ID . '|Google+Drive|' .
  181. \repository_googledocs::MY_DRIVE_ROOT_ID . '|My+Drive';
  182. $node = helper::get_node($gdcontent, $path);
  183. // Assert that the returned content node class by get_node() is equal to the expected one.
  184. $this->assertEquals($expected, get_class($node));
  185. }
  186. /**
  187. * Data provider for test_get_node().
  188. *
  189. * @return array
  190. */
  191. public function get_node_provider(): array {
  192. return [
  193. 'The content object represents a Google Drive folder.' =>
  194. [
  195. $this->create_google_drive_folder_object('e3b0c44298fc1c149', 'Folder', ''),
  196. \repository_googledocs\local\node\folder_node::class,
  197. ],
  198. 'The content object represents a Google Drive file.' =>
  199. [
  200. $this->create_google_drive_file_object('de04d58dc5ccc', 'File.pdf',
  201. 'application/pdf'),
  202. \repository_googledocs\local\node\file_node::class,
  203. ],
  204. ];
  205. }
  206. /**
  207. * Test request() when an exception is thrown by the API call.
  208. *
  209. * @dataProvider request_exception_provider
  210. * @param \Exception $exception The exception thrown by the API call
  211. * @param \Exception $expected The expected exception thrown by request()
  212. */
  213. public function test_request_exception(\Exception $exception, \Exception $expected) {
  214. // Mock the service object.
  215. $servicemock = $this->createMock(rest::class);
  216. // Assert that the call() method is being called only once with the given arguments.
  217. // Define the thrown exception by this call.
  218. $servicemock->expects($this->once())
  219. ->method('call')
  220. ->with('list', [])
  221. ->willThrowException($exception);
  222. $this->expectExceptionObject($expected);
  223. helper::request($servicemock, 'list', []);
  224. }
  225. /**
  226. * Data provider for test_request_exception().
  227. *
  228. * @return array
  229. */
  230. public function request_exception_provider(): array {
  231. return [
  232. 'The API call throws exception (status: 403; message: Access Not Configured).' =>
  233. [
  234. new \Exception('Access Not Configured', 403),
  235. new \repository_exception('servicenotenabled', 'repository_googledocs'),
  236. ],
  237. 'The API call throws exception (status: 405; message: Access Not Configured).' =>
  238. [
  239. new \Exception('Access Not Configured', 405),
  240. new \Exception('Access Not Configured', 405),
  241. ],
  242. 'The API call throws exception (status: 403; message: Access Forbidden).' =>
  243. [
  244. new \Exception('Access Forbidden', 403),
  245. new \Exception('Access Forbidden', 403),
  246. ],
  247. 'The API call throws exception (status: 404; message: Not Found).' =>
  248. [
  249. new \Exception('Not Found', 404),
  250. new \Exception('Not Found', 404),
  251. ],
  252. ];
  253. }
  254. }