BackendTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Files_Sharing\Tests;
  28. /**
  29. * Class BackendTest
  30. *
  31. * @group DB
  32. */
  33. class BackendTest extends TestCase {
  34. const TEST_FOLDER_NAME = '/folder_share_api_test';
  35. public $folder;
  36. public $subfolder;
  37. public $subsubfolder;
  38. protected function setUp() {
  39. parent::setUp();
  40. $this->folder = self::TEST_FOLDER_NAME;
  41. $this->subfolder = '/subfolder_share_backend_test';
  42. $this->subsubfolder = '/subsubfolder_share_backend_test';
  43. $this->filename = '/share-backend-test.txt';
  44. // save file with content
  45. $this->view->file_put_contents($this->filename, $this->data);
  46. $this->view->mkdir($this->folder);
  47. $this->view->mkdir($this->folder . $this->subfolder);
  48. $this->view->mkdir($this->folder . $this->subfolder . $this->subsubfolder);
  49. $this->view->file_put_contents($this->folder.$this->filename, $this->data);
  50. $this->view->file_put_contents($this->folder . $this->subfolder . $this->filename, $this->data);
  51. $this->view->file_put_contents($this->folder . $this->subfolder . $this->subsubfolder . $this->filename, $this->data);
  52. }
  53. protected function tearDown() {
  54. if ($this->view) {
  55. $this->view->unlink($this->filename);
  56. $this->view->deleteAll($this->folder);
  57. }
  58. parent::tearDown();
  59. }
  60. public function testGetParents() {
  61. $fileinfo1 = $this->view->getFileInfo($this->folder);
  62. $fileinfo2 = $this->view->getFileInfo($this->folder . $this->subfolder . $this->subsubfolder);
  63. $fileinfo3 = $this->view->getFileInfo($this->folder . $this->subfolder . $this->subsubfolder . $this->filename);
  64. $this->assertTrue(\OC\Share\Share::shareItem('folder', $fileinfo1['fileid'], \OCP\Share::SHARE_TYPE_USER,
  65. self::TEST_FILES_SHARING_API_USER2, 31));
  66. $this->assertTrue(\OC\Share\Share::shareItem('folder', $fileinfo2['fileid'], \OCP\Share::SHARE_TYPE_USER,
  67. self::TEST_FILES_SHARING_API_USER3, 31));
  68. $backend = new \OCA\Files_Sharing\ShareBackend\Folder();
  69. $result = $backend->getParents($fileinfo3['fileid']);
  70. $this->assertSame(2, count($result));
  71. $count1 = 0;
  72. $count2 = 0;
  73. foreach($result as $r) {
  74. if ($r['path'] === 'files' . $this->folder) {
  75. $this->assertSame(ltrim($this->folder, '/'), $r['collection']['path']);
  76. $count1++;
  77. } elseif ($r['path'] === 'files' . $this->folder . $this->subfolder . $this->subsubfolder) {
  78. $this->assertSame(ltrim($this->subsubfolder, '/'), $r['collection']['path']);
  79. $count2++;
  80. } else {
  81. $this->assertTrue(false, 'unexpected result');
  82. }
  83. }
  84. $this->assertSame(1, $count1);
  85. $this->assertSame(1, $count2);
  86. $result1 = $backend->getParents($fileinfo3['fileid'], self::TEST_FILES_SHARING_API_USER3);
  87. $this->assertSame(1, count($result1));
  88. $elemet = reset($result1);
  89. $this->assertSame('files' . $this->folder . $this->subfolder . $this->subsubfolder ,$elemet['path']);
  90. $this->assertSame(ltrim($this->subsubfolder, '/') ,$elemet['collection']['path']);
  91. }
  92. }