PropagationTestCase.php 2.7 KB

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