1
0

PropagationTestCase.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\Tests;
  8. abstract class PropagationTestCase extends TestCase {
  9. /**
  10. * @var \OC\Files\View
  11. */
  12. protected $rootView;
  13. protected $fileIds = []; // [$user=>[$path=>$id]]
  14. protected $fileEtags = []; // [$id=>$etag]
  15. public static function setUpBeforeClass(): void {
  16. parent::setUpBeforeClass();
  17. \OCA\Files_Sharing\Helper::registerHooks();
  18. }
  19. protected function setUp(): void {
  20. parent::setUp();
  21. $this->setUpShares();
  22. }
  23. protected function tearDown(): void {
  24. \OC_Hook::clear('OC_Filesystem', 'post_write');
  25. \OC_Hook::clear('OC_Filesystem', 'post_delete');
  26. \OC_Hook::clear('OC_Filesystem', 'post_rename');
  27. \OC_Hook::clear('OCP\Share', 'post_update_permissions');
  28. parent::tearDown();
  29. }
  30. abstract protected function setUpShares();
  31. /**
  32. * @param string[] $users
  33. * @param string $subPath
  34. */
  35. protected function assertEtagsChanged($users, $subPath = '') {
  36. $oldUser = \OC::$server->getUserSession()->getUser();
  37. foreach ($users as $user) {
  38. $this->loginAsUser($user);
  39. $id = $this->fileIds[$user][$subPath];
  40. $path = $this->rootView->getPath($id);
  41. $etag = $this->rootView->getFileInfo($path)->getEtag();
  42. $this->assertNotEquals($this->fileEtags[$id], $etag, 'Failed asserting that the etag for "' . $subPath . '" of user ' . $user . ' has changed');
  43. $this->fileEtags[$id] = $etag;
  44. }
  45. $this->loginAsUser($oldUser->getUID());
  46. }
  47. /**
  48. * @param string[] $users
  49. * @param string $subPath
  50. */
  51. protected function assertEtagsNotChanged($users, $subPath = '') {
  52. $oldUser = \OC::$server->getUserSession()->getUser();
  53. foreach ($users as $user) {
  54. $this->loginAsUser($user);
  55. $id = $this->fileIds[$user][$subPath];
  56. $path = $this->rootView->getPath($id);
  57. $etag = $this->rootView->getFileInfo($path)->getEtag();
  58. $this->assertEquals($this->fileEtags[$id], $etag, 'Failed asserting that the etag for "' . $subPath . '" of user ' . $user . ' has not changed');
  59. $this->fileEtags[$id] = $etag;
  60. }
  61. $this->loginAsUser($oldUser->getUID());
  62. }
  63. /**
  64. * Assert that the etags for the root, /sub1 and /sub1/sub2 have changed
  65. *
  66. * @param string[] $users
  67. */
  68. protected function assertEtagsForFoldersChanged($users) {
  69. $this->assertEtagsChanged($users);
  70. $this->assertEtagsChanged($users, 'sub1');
  71. $this->assertEtagsChanged($users, 'sub1/sub2');
  72. }
  73. protected function assertAllUnchanged() {
  74. $users = [self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2,
  75. self::TEST_FILES_SHARING_API_USER3, self::TEST_FILES_SHARING_API_USER4];
  76. $this->assertEtagsNotChanged($users);
  77. }
  78. }